LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Execute commands from an input file (https://www.linuxquestions.org/questions/linux-newbie-8/execute-commands-from-an-input-file-934646/)

mfarch99 03-15-2012 03:14 PM

Execute commands from an input file
 
I know this is probably a very simple task but I'm new to this and have been trying to understand how to accomplish the following. I have a list of commands that are stored in a file. Lets call that input1. I want to take that file (input1) and execute every one of those commands from a script. The output will all go to a single file. What I have tried is the following:
#!/bin/bash


cat /root/scripts/binary_list.txt | while read i
do
$i >> /tmp/ASYS/RO33730/patchlvlout.out 2>&1
echo $i -- This is just to see what is comming thru the file.
done

Every time i run this I receive an error stating that command cannot be found.

If I string all the commands in a script without pulling them thru a file or putting in a loop the commands work ok. The proiblem seems to be when using the file as input I must be missing something. Any help would be appreciated. Thanks.

T3RM1NVT0R 03-15-2012 03:55 PM

@ Reply
 
Hi mfarch99,

Welcome to LQ!!!

As you said that the commands work when you put them in a file and then run that file as a script. However, it does not work when you use this file as an input. If that is the case then the following script should do:

Code:

#!/bin/bash
for i in `cat input-file`
do
$i
done

The above example is only applicable when you are using just commands (without any switches) if you are planning to put switches then it will not work, the reason being it will take switch as a seperate string.

dwduback 03-15-2012 04:04 PM

Executing commands and redirecting to output file
 
I have tried your script; it works fine.

It is possible that the directory you have the script file in is not in your path. My script is stored in 'exlist' (no quotes).

To execute it, I navigate to the directory where exlist is stored and type './exlist' (again, no quotes).

Hope this helps...

Dave

mfarch99 03-15-2012 04:28 PM

Thank you for the responses. The input file was bad. I re-created the file and the script worked fine. Again, Thank you for the responses..

mfarch99 03-15-2012 04:32 PM

Here is another question on this. IS there a way to print the command that is executing the file into the output file? In other words, every time the command is executed I want to see the command and the output from that command in one output file.

T3RM1NVT0R 03-15-2012 04:43 PM

@ Reply
 
Just use echo $i and then $i as follows (copied your script as an example):

Code:

cat /root/scripts/binary_list.txt | while read i
do
echo $i >> /tmp/ASYS/RO33730/patchlvlout.out 2>&1
$i >> /tmp/ASYS/RO33730/patchlvlout.out 2>&1
echo $i -- This is just to see what is comming thru the file.
done


xeleema 03-15-2012 05:06 PM

Greetingz!

Try this, instead;

Code:

if [ -r ./commands.txt ]; then
  while read line
  do
      ${line} 2>&1 | /usr/bin/tee -a ./commands.out
      echo ${line}
  done < ./commands.txt
fi

Note that I wrapped the while-loop in an if-statement to give some semblence of sanity-checking. If this is indeed running as root, then you're going to want to make sure that no one throws something crazy in the input file (like 'rm -rf /').

devinwhite717 03-20-2012 11:07 AM

More linux commands
 
Here is a list of the most used linux commands that will help you to write shell programs


All times are GMT -5. The time now is 02:13 AM.