Unix commands in one line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Unix commands in one line
# 1  
Old 11-03-2008
Unix commands in one line

Hi I want to write the following code in 1 line:

a=1
if [ a -eq 1 ]
then
echo ok
else
echo not ok
fi

The following does not work:

a=1 ; if [ a -eq 1 ] \r then \r echo ok \r else \r echo not ok \r fi

or

Even if I replace \r by \n it does not work.

Please suggest.
# 2  
Old 11-03-2008
Just replace the \r's with ;s.
# 3  
Old 11-03-2008
Code:
a=1; [[ $a -eq 1 ]] && echo "ok"; [[ $a -ne 1 ]] && echo "no okay"

One way.
FWIW writing code like thise makes it hard to read. hard to read == hard to debug and fix

Last edited by jim mcnamara; 11-03-2008 at 09:08 AM..
# 4  
Old 11-03-2008
Commands in one line

if vi is the default editor than :

1)write a=1
2) pass to command mode (key esc)
3) press key v
4) write your follwing code
5) save and quit the editor
# 5  
Old 11-03-2008
Thanks...All..
But I got the following error:

a=1; [[ $a -eq ]] && echo "ok"; [[ $a -ne 1 ]] && echo "no okay"
ksh: syntax error: `]]' unexpected

I was trying something like the following in one line:

if [ -f ./test_file_file ]
then
echo "file there"
else
echo "not there"
fi
# 6  
Old 11-03-2008
Jim Mcnamara,

Thanks it worked.

a=1; [[ $a -eq ]] && echo "ok"; [[ $a -ne 1 ]] && echo "no okay"

I didnt put $a -eq 1 in the above code.
1 was missing.

Thanks..
# 7  
Old 11-03-2008
Jim,

I have modified your code to the following:

ls ./test_file_file ; b=`echo $?` ; [[ b -eq 0 ]] && echo "file there" ; [[ b -ne 0 ]] && echo "file not there"

It worked...
Many thanks..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Reading ls -l output line by line awk the user name and su user to run commands

Using ksh on AIX what I am trying to do is to read the ls -l output from a file in a do while loop line by line. Extract the user name(3rd field) and the directory/file name(9th field) using awk and save them into variables. su -c to the user and change directory/file permisions to 777. Script I... (13 Replies)
Discussion started by: zubairom
13 Replies

2. UNIX for Dummies Questions & Answers

Multiple Commands on a Single Line

Hi There, I have a cronjob that executes a small script (few lines) that I am certain can be achieved in a single line. The functional objective is actually really simple; cmd var1 The '1' in 'var1' is actually derived from date (day of month) but the snag is when working with 1-9 I... (3 Replies)
Discussion started by: Random79
3 Replies

3. UNIX for Dummies Questions & Answers

How do you use UNIX commands in the Windows Command Line?

I tried opening the windows command line and typing UNIX commands, but they don't work. It kept saying that it was not recognized as an internal or external command, operable program or batch file. Removed strange sentence with even stranger link I need to use UNIX commands such as 'less',... (9 Replies)
Discussion started by: vlay2
9 Replies

4. UNIX for Dummies Questions & Answers

Running two commands in one line

hi! how do i run 2 command from the same line e.g: 'which screen' and then ls -la 'which screen' (4 Replies)
Discussion started by: rdns
4 Replies

5. UNIX for Dummies Questions & Answers

How to get nth line from a file usuing commands

Hi to all, I have a file with 1000 lines,Now i need to get 789th record.So please any one help me out from this. Thanks in advance. Sathish (3 Replies)
Discussion started by: bsathishmca
3 Replies

6. Shell Programming and Scripting

Seperate commands on the same line

hello, is there a way to seperate commands that are on the same line? ie: echo "yes count(*)>0:" $passvar > dsp cat dsp cat dsp > log i'm trying to put these 3 commands on the same line, but when i do they all get concatenated into 1 lieteral if i put the cat commands on... (3 Replies)
Discussion started by: bobk544
3 Replies

7. UNIX for Dummies Questions & Answers

Running UNIX commands remotely in Windows box from Unix box – avoid entering password

I am able to run the UNIX commands in a Windows box from a UNIX box through "SSH" functionality. But whenever the SSH connection is established between UNIX and Windows, password for windows box is being asked. Is there a way to avoid asking password whenever the SSH connection is made? Can I... (1 Reply)
Discussion started by: D.kalpana
1 Replies

8. Shell Programming and Scripting

Commands and Command Line Flags

Hello I'm very new to unix, am learning it as part of my new job. I have a book which has a list in the back, of a number of commands and their command line flags, detailing what these flags are used for. I've been looking everywhere for an electronic copy of this type of list, so I can... (4 Replies)
Discussion started by: Kedgeboy
4 Replies

9. UNIX for Advanced & Expert Users

two commands on 1 line

wuts the command that allows u to use 2 commands on one line? (2 Replies)
Discussion started by: el_soldado
2 Replies

10. UNIX for Dummies Questions & Answers

Maingrame to UNIX sending UNIX commands

I want to know if there is a way to send unix commands thru FTP from a mainframe to kick off Autosys Jobs. I just need to send a command from the mainframe to UNIX and have UNIX execute that command. (2 Replies)
Discussion started by: skammer
2 Replies
Login or Register to Ask a Question