Link to home
Start Free TrialLog in
Avatar of mlcarey1959
mlcarey1959

asked on

How do I combine several shell commands on one line?

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?  
Avatar of Giovanni
Giovanni
Flag of United States of America image

@echo off&echo hello.&echo goodbye.
@echo off&echo hello.&echo goodbye.

Open in new window

SOLUTION
Avatar of Giovanni
Giovanni
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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

Avatar of mlcarey1959
mlcarey1959

ASKER

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
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
This works

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