How do I combine several shell commands on one line?

mlcarey1959
mlcarey1959 used Ask the Experts™
on
for f in *.c *h; do echo >> $$f; done


tail -1 $$ff | od -An -t -oC -w1 | tail -1 | grep -c "012"

How do I combine these two commands?  
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
@echo off&echo hello.&echo goodbye.
@echo off&echo hello.&echo goodbye.

Open in new window

you can run multiple commands on a single line by separating the commands by a semi colon.

example:

echo $PWD ; ls -a

You can also run multiple commands on a single line via a pipe. The pipe takes the output of one command and "pipes" it as input to another command.

example:

ps -ef | grep ORA

Also, if you want to conditionally execute the second command based on the return code of the first you can use || or &&.

<command1> && <command2> will execute command2 if command1 returns a value >= 0 (ie success).

<command1> && <command2> will exeucte command2 if command1 returns a value < 0 (ie failure)
Top Expert 2011

Commented:
Let me clear your requirement. You want to

append the output of
tail -1 $f | od -An -t -oC -w1 | tail -1 | grep -c "012"
into $f

such as

for f in *.c *h; do echo `tail -1 $f | od -An -t -oC -w1 | tail -1 | grep -c "012"` >> $f; done

Open in new window

Author

Commented:
I need to loop through some source files to check for a new line character at the end of the file and append a new line if one does not exist.



for f in *.c *.h; do if test (tail -1 $f  | od -An -t -oC -w1 | tail -1 | grep -c "012") == 0 then echo >> "$$f" fi; done
Top Expert 2011
Commented:

for f in *.c *.h; do [[ tail -1 $f  | od -An -t -oC -w1 | tail -1 | grep -c "012" ]] && echo >> "$f" fi; done

Open in new window

Author

Commented:
This does not work as is.
for f in *.c *.h; do [[ tail -1 $f  | od -An -t -oC -w1 | tail -1 | grep -c "012" ]] && echo >> "$f" fi; done

conditional binary operator expected
syntax error near `-1'


for f in *.c *.h; do  tail -1 $f  | od -An -t oC -w1 | tail -1 | grep -c "012" || echo >> "$f"; done
This prints 0 if the file does not have a new line character and appends a new line character to the file.
It should only append the new line character.

Top Expert 2011
Commented:
[[ tail -1 $f  | od -An -t -oC -w1 | tail -1 | grep -c "012" ]]
is the same as
test (  tail -1 $f  | od -An -t -oC -w1 | tail -1 | grep -c "012" )

So if the return value of the following commands is true "0"
tail -1 $f  | od -An -t -oC -w1 | tail -1 | grep -c "012"

  then, with &&, echo >> $f   is executed.
        , with ||, echo >> $f  won't be executed.

Author

Commented:
This works

for f in *.c *.h; do [[ $( tail -1 $f | od -An -t oC -w1 | tail -1 | grep -c "012") ]] && echo >> "$f"; done

Author

Commented:
no reason

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial