Reputation: 225
I am running this command
awk '{print "Removing " ORS $0;system("rm " $0 ORS " if [ $? -eq 0 ]" ORS "then" ORS "echo file " $0 " has been removed." ORS "fi")}' <(cat /tmp/findo)
On bash and the command works however when executed from a shell script it throws the underlying error
Here is the example and you may look at the 'line 23 error'
# sh -x rvarlog_util.sh
+ findout=/tmp/findout
+ '[' -e /tmp/findout ']'
++ du -sm /var/log
++ awk '{print $1+0}'
+ cdu=2372
++ awk '{print $1+0}'
++ grep total
++ du -m --max-depth=1 -c -m -x /var/log/messages /var/log/messages-20190310 /var/log/messages-20190323-1553338190.gz /var/log/messages-20190324-1553424406.gz /var/log/messages-20190324-1553456095.gz /var/log/messages-20190324-1553456293.gz /var/log/messages-20190324-1553457237.gz /var/log/messages-20190324-1553457268.gz /var/log/maillog-20190324-1553456095.gz /var/log/boot.log /var/log/audit/audit.log /var/log/audit/audit.log-20190311-1552325090.gz /var/log/puppetlabs
+ fusage=2258
rvarlog_util.sh: line 23: syntax error near unexpected token `('
rvarlog_util.sh: line 23: `awk '{print "Removing " ORS $0;system("rm " $0 ORS " if [ $? -eq 0 ]" ORS "then" ORS "echo file " $0 " has been removed." ORS "fi")}' <(cat /tmp/findo)'
Upvotes: 1
Views: 1193
Reputation: 125708
@Ibraheem has the right solution, but so far nobody's spotted the problem. It's that you're using a process substitution (<(cat /tmp/findo)
), but running the script with sh
rather than bash
. Process substitution isn't available in all shells (or even bash when it's invoked as "sh").
There are a couple of ways to fix this, and I'd recommend doing both ('cause they're good ideas on their own):
Don't use <(cat somefile)
, use a plain redirect, like <somefile
. The process-substituted cat
command is an overly complex, fragile, and inefficient way to read from a file.
Give the script a proper shebang line (#!/bin/bash
or #!/usr/bin/env bash
), make it executable (chmod +x rvarlog_util.sh
), and run it directly by entering its path (./rvarlog_util.sh
) rather than explicitly specifying a shell (sh
or bash
). In general, the script should "know" which shell it's written for, and overriding that (by explicitly specifying a shell when you run it) is a bad idea.
Upvotes: 3
Reputation: 149
from your awk command, what I understood that you are trying to remove files who's names found in /tmp/findo file, is that correct? then replace your awk command with below code and see if it works, BUT make sure that the file names in /tmp/findo
contain absolute path to the required files you are trying to remove
while read -r files
do
rm "$files"
if [ $? -eq 0 ]
then
"echo $files has been removed."
fi
done < /tmp/findo
Upvotes: 2