LinuxQuestions.org
Visit Jeremy's Blog.
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 09-17-2014, 11:55 PM   #1
LYC
Member
 
Registered: Jan 2014
Posts: 100

Rep: Reputation: Disabled
Scripting queries - Newbie


Hi,

I have a file containing a list of commands with argument, how to run the commands automatically?

Assuming the contents of file "command2run" are as follows:
ls -ls
ls -la
ls -L

I tried the following:
for i in `cat command2run`
do
$i
done

The arguments were ran seperately instead of "ls -ls". Please advise?

Thanks in advance.
 
Old 09-18-2014, 12:29 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,255

Rep: Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686
Ok ... a few things:

1. Please use [code][/code] tags around code and data

2. Do not use a for loop to read a file as it will under go word splitting (which is why you see separate entries). Use a while loop

3. If you are looking to execute the items in the file you can source it, ie use source or . prior to file name on the command line
Code:
$ . command2run
 
Old 09-18-2014, 11:34 PM   #3
LYC
Member
 
Registered: Jan 2014
Posts: 100

Original Poster
Rep: Reputation: Disabled
Hi,

I used . command2run but it just not able to go back to the command prompt after it finished. Not even I do and control-c.
 
Old 09-19-2014, 12:31 AM   #4
SAbhi
Member
 
Registered: Aug 2009
Location: Bangaluru, India
Distribution: CentOS 6.5, SuSE SLED/ SLES 10.2 SP2 /11.2, Fedora 11/16
Posts: 664

Rep: Reputation: 80
Then use "source" Or a little help for you to use while to read a file:

Code:
while read line; do <do something here with $line>; done< input_filename

Last edited by SAbhi; 09-19-2014 at 12:33 AM.
 
Old 09-19-2014, 02:33 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,255

Rep: Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686
Ok, not sure what to tell you as it works as described for me.

However, here are some assumptions I have made which may be incorrect:

1. Your standard shell is bash, to check:
Code:
$ echo $SHELL
/bin/bash
2. Your file contains the following data:
Code:
$ cat command2run
ls -ls
ls -la
ls -L
3. When run I receive:
Code:
$ . command2run
total 16
12 -rwxr-xr-x 1 grail users 10444 17.09.2014 18:07 bashrc.tar.gz*
 4 -rwx------ 1 grail users    46 17.09.2014 18:28 d.sh*
total 64
drwxr-xr-x 2 grail users  4096 17.09.2014 18:26 ./
drwxr-xr-x 4 grail users  4096 19.09.2014 14:22 ../
-rw------- 1 grail users 19679 14.09.2014 16:04 .bashrc
-rw------- 1 grail users 11025 14.09.2014 15:24 .bashrc_help
-rw------- 1 grail users  7828 14.09.2014 15:24 .bashrc_help.no_color
-rwxr-xr-x 1 grail users 10444 17.09.2014 18:07 bashrc.tar.gz*
-rwx------ 1 grail users    46 17.09.2014 18:28 d.sh*
bashrc.tar.gz*  d.sh*
Obviously your output will be different
 
Old 09-19-2014, 06:49 AM   #6
kaushalpatel1982
Member
 
Registered: Aug 2007
Location: INDIA
Distribution: CentOS, RHEL, Fedora, Debian, Ubuntu, LinuxMint, PCLinuxOS
Posts: 153

Rep: Reputation: 9
run it with sh command directly

Quote:
sh command2run
 
  


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
Newbie patching queries - RedHat LYC Linux - Newbie 1 03-04-2014 02:20 PM
queries abut awk scripting mbalaji Linux - Newbie 2 10-17-2012 03:13 AM
Mixed bag of Newbie Queries. 0rinoco Linux - Newbie 29 05-19-2012 02:44 AM
Multiple linux-newbie queries nardz Linux - Newbie 9 03-29-2007 11:22 AM
newbie webserver queries. Trinity22 Linux - Newbie 5 08-22-2003 03:04 PM


All times are GMT -5. The time now is 10:29 AM.

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