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.

Just replace the \r's with ;s.

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

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

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

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..

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..

echo "file$([[ ! -f ./test_file_file ]] && echo " not") there"

If I was going to use && I would use || as well rather than doing the test twice. Also no need to assign $? to a variable, as that is the result that && and || calculate their logic on anyway:

ls ./test_file_file && echo "file there" || echo "file not there"

I wouldn't use ls; it is unnecessary for testing the file's existence. I'd use the shell's built-in test:

[ -f ./test_file_file ] && echo "file there" || echo "file not there"

I agree, unless you wanted the output of the ls command to be displayed as well for some reason.

In which case it would be faster to use echo or printf to display the name of the file; there's no need to use an external command to do it.