LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-15-2012, 04:14 PM   #1
mfarch99
LQ Newbie
 
Registered: Mar 2012
Posts: 20

Rep: Reputation: Disabled
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.
 
Old 03-15-2012, 04:55 PM   #2
T3RM1NVT0R
Senior Member
 
Registered: Dec 2010
Location: Internet
Distribution: Linux Mint, SLES, CentOS, Red Hat
Posts: 2,385

Rep: Reputation: 477Reputation: 477Reputation: 477Reputation: 477Reputation: 477
@ 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.
 
Old 03-15-2012, 05:04 PM   #3
dwduback
LQ Newbie
 
Registered: Nov 2005
Location: Glenview, IL
Posts: 10

Rep: Reputation: 1
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
 
Old 03-15-2012, 05:28 PM   #4
mfarch99
LQ Newbie
 
Registered: Mar 2012
Posts: 20

Original Poster
Rep: Reputation: Disabled
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..
 
Old 03-15-2012, 05:32 PM   #5
mfarch99
LQ Newbie
 
Registered: Mar 2012
Posts: 20

Original Poster
Rep: Reputation: Disabled
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.
 
Old 03-15-2012, 05:43 PM   #6
T3RM1NVT0R
Senior Member
 
Registered: Dec 2010
Location: Internet
Distribution: Linux Mint, SLES, CentOS, Red Hat
Posts: 2,385

Rep: Reputation: 477Reputation: 477Reputation: 477Reputation: 477Reputation: 477
@ 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
 
Old 03-15-2012, 06:06 PM   #7
xeleema
Member
 
Registered: Aug 2005
Location: D.i.t.h.o, Texas
Distribution: Slackware 13.x, rhel3/5, Solaris 8-10(sparc), HP-UX 11.x (pa-risc)
Posts: 987
Blog Entries: 4

Rep: Reputation: 252Reputation: 252Reputation: 252
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 /').

Last edited by xeleema; 03-15-2012 at 06:07 PM. Reason: Wow, I really need to hit F5 before replying to a thread.
 
Old 03-20-2012, 12:07 PM   #8
devinwhite717
LQ Newbie
 
Registered: Mar 2012
Posts: 10

Rep: Reputation: 1
More linux commands

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


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
gcc will not execute, responds with 'no such file or directory' and 'no input file' nckeecho Ubuntu 9 07-24-2016 02:04 PM
[SOLVED] Read and execute set of linux commands from file kirukan Linux - Newbie 5 09-07-2011 03:54 AM
how to use expect - send to execute list of commands inside a file auma78 Linux - General 1 02-02-2011 01:00 PM
[SOLVED] Can't Execute Commands as sudo Nor Access Sudoers File in Mandriva 2010.1 gdawg Linux - Newbie 4 08-17-2010 02:32 PM
how to create execute file for linux telnet commands siva karthik Programming 7 03-10-2006 01:50 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 08:08 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration