Pass CAT comand as argument to function
I'm trying to write a very simple bash script that modifies a number of
files, and I'm outputting the results of each command to a log as a check
whether the command was completed successfully. Everything appears to be
working except I can't pass CAT with variables to my script -- I keep
getting a cat: >>: No such file or directory error.
#! /bin/bash
check () {
if ( $1 > /dev/null ) then
echo " $1 : completed" | tee -a log
return 0;
else
echo "ERR> $1 : command failed" | tee -a log
return 1;
fi
}
check "cp $file $file.bak" # this works fine
check "sed -i s/text/newtext/g $file" # this works, too
check "cat $file1 >> $file2" # this does not work
I've tried any number of combinations of quoting the command. The only way
that I can get it to work is by using the following:
check $(cat $file1 >> $file2)
However, this does not pass the command itself to check only the return
value, so $1 in function check carries /dev/null and not the command
performed, which is not the particular behaviour I want.
Just for completeness, the log file looks like:
cp ./file1 ./file1.bak : completed
sed -i s/text/newtext/g ./file1 : completed
ERR> cat ./file1 >> ./file2 : command failed
I'm sure the solution is rather simple, but it has eluded me for a few
hours and no amount of Google searches has yielded any help. Thanks for
having a look.
No comments:
Post a Comment