0

I have saved the commands in a file and trying to read it from another file. commands without a pipe are working fine but wherever there is a pipe in command it is not getting executed.

can anyone help me out?

Script

#!/bin/bash
rm -f /ZLD_Implement/ZLD_Implement.txt
while IFS=#, read -r Command_info1 Command; do
     variable1=$Commandenter code here
echo "$Command_info1" >> /ZLD_Implement/ZLD_Implement.txt
echo "`$variable1`"  >> /ZLD_Implement/ZLD_Implement.txt
done < /ZLD_Implement/ZLD_Command_list.txt

Command list:

[root@localhost ZLD_Implement]# cat ZLD_Command_list.txt
Command to check the hostname # hostname
Command to check the linux kernel version # uname -a
Command to check the current system date # dateer code here
Command to check Disk space # df -kh
Command to cheeck routes in Netstat # netstat -nrv
command to check audit logs # ausearch -i -m USER_CHAUTHTOK,USER_MGMT,USER_AUTH` -if /var/log/audit/audit.log|tail -50nter code here
  • The way that you've piped to tail doesn't look correct. Is it a typo in your question or are you literally using |tail -50nter ? – Jeff H. Jun 22 at 23:48
  • thats a typo -jeff the actual command ends with tail -50 – Bharath S.V Jun 23 at 23:58
1

The shell parses pipes (and redirects and quotes and escapes and...) before expanding variables, so if you put a pipe (or redirect or...) in a variable, by the time it's expanded it's too late for it to function normally; at that point, it's just a regular argument to a command. See BashFAQ #50: I'm trying to put a command in a variable, but the complex cases always fail!

The solution: In this case, I'd actually recommend using eval. eval has a really bad reputation as a bug magnet, because it's easy to misuse it in dangerous ways. Basically, its purpose is to execute data as though it were shell syntax and commands, and that means the line between executable code and inert data (like filenames) is even weaker than it normally is. But in this case, the data read from ZLD_Command_list.txt is supposed to be treated as shell syntax and commands. If there's an rm -R / in the file, it's supposed to delete the entire filesystem.

However, you should still observe proper quoting. That means double-quoting variables, like eval "$variable1" instead of just eval $variable1. Also, I recommend using $() instead of backticks (see BashFAQ #82), but since you're just going to echo the result, why not just execute the command directly?

Other possible optimizations: Rather than copying the value of $Command to $variable1, just use it directly. Rather than redirecting output from each command separately, why not redirect output from the entire loop?

Here's my version, with all of the above fixed:

#!/bin/bash
while IFS=#, read -r Command_info1 Command; do
    echo "$Command_info1"
    eval "$Command"
done < /ZLD_Implement/ZLD_Command_list.txt > /ZLD_Implement/ZLD_Implement.txt

Oh, and do you want to capture error output as well as standard output from the commands? If so, add 2>&1 after the final redirect.

  • thanks Gordon, eval solved the issue. is there a better way to execute the set of CLI commands since the requirement is to automate the task of executing the commands manually.(I should also be able to execute a different set of commands without modifying the script). – Bharath S.V Jun 24 at 0:08
  • @BharathS.V Why not just create a regular script for each set of commands you want to execute? – Gordon Davisson Jun 24 at 6:24
  • I don't want people making unnecessary changes to the script whenever there is a requirement to add additional commands to the list, so the idea is to add/remove commands from the files wherever necessary. Also if someone needs a totally new set of commands to be executed then I'll have to write a different script which is time-consuming and creates dependency. hence the Idea of having a file with the list of commands which needs to be executed. (the script will eventually run from a non-root user which will handle command permission issues(i.e what to execute and what not to)) – Bharath S.V Jun 24 at 20:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.