How to execute a command inside a while loop?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to execute a command inside a while loop?
# 1  
Old 01-03-2014
How to execute a command inside a while loop?

How do we execute a command inside a while loop?
# 2  
Old 01-03-2014
Code:
while <condition>; do
  <command>
done

Example:

Code:
$ i=0
$ while ((i++ < 3)); do
>   echo $i
> done
1
2
3

# 3  
Old 01-03-2014
hey sorry, i wrote the description but don't lnow how it got erased.

i have a set of commands inside a text file. 1 command per line.
command.txt
Code:
echo "start"
ls -l
pwd

i want to execute these commands one by one and after finishing the last command, again it should start from the 1st command.

infinity.sh
Code:
count=0
while :
do
        while read exe_command
        do
                (( count++ ));
                echo "$exe_command"
                result_$count=`$exe_command`
        done < command.txt
        echo ""
done

the commands are not getting executed.

output:
e
Code:
cho "start"
infinite.sh: line 8: result_178="start": command not found
ls -l
infinite.sh: line 8: result_179=total: command not found
pwd
infinite.sh: line 8: result_180=/home: No such file or directory

# 4  
Old 01-03-2014
The easiest way is:
Code:
sh command.txt

# 5  
Old 01-03-2014
Quote:
Originally Posted by radoulov
The easiest way is:
Code:
sh command.txt

I don't think that will work.
Its a txt file. sh cannot run a .txt file
# 6  
Old 01-03-2014
Have you tried it?
This User Gave Thanks to radoulov For This Post:
# 7  
Old 01-03-2014
So, you are trying to set a variable variable name then. Have you considered using an array?
Code:
#!/bin/ksh93
set -A result

count=0
while :
do
        while read exe_command
        do
                (( count++ ));
                echo "$exe_command"
                result[$count]=`$exe_command`
        done < command.txt
        echo ""
done

for i in 1 2 3 4 5
do
   echo "Result for $i is ${result[$i]}"
done

Of course, this is assuming that you have a simple value to store in your variable. The output you have given us suggests that you want to store the output from ls -l in a variable. I'm not really sure that is a good thing to attempt.

Are you actually after storing the return code instead? In that case, change the line:-
Code:
            result[$count]=`$exe_command`

to be:-
Code:
            $exe_command
            result[$count]=$?

There is also a limit to the number of items in the array, 0-1023 in ksh, 4 million in ksh93. Not sure on the bash limit.


WARNING - your logic is dangerous.

Imagine if someone could infect your input file and add any of the following:-
Code:
shutdown -t 0
rm /etc/inittab
rm -r /etc/rc.d
rm /etc/passwd
rm -r /




I hope that this helps / gives you something bigger to consider.

Robin
This User Gave Thanks to rbatte1 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

CUT command not giving correct result inside loop

Hi, i have a source file and have 3 columns and separated by "|" .i want to split this 3 columns in different variable.When i am executing this values indivisually giving correct result but when the same execute inside a for loop,it's giving issues. Src file(jjj.txt) -------... (8 Replies)
Discussion started by: raju2016
8 Replies

2. Shell Programming and Scripting

While loop breaking when using "ssh" command inside

Hi , I am trying to read a list of hosts from a config file and trying to get file list from that host. For this I have used one while loop. cat "$ARCHIVE_CFG_FILE" | sed '/^$/d' | sed '/^#/d' | while read ARCHIVE_CFG do SOURCE_SERVER_NAME=`echo "$ARCHIVE_CFG" | awk -F '|' '{ print... (2 Replies)
Discussion started by: Anupam_Halder
2 Replies

3. Shell Programming and Scripting

If else condition inside for loop of awk command in UNIX shell scripting

Hi , Please excuse me for opening a new thread i am unable to find out the syntax error in my if else condition inside for loop in awk command , my actual aim is to print formatted html td tag when if condition (True) having string as "failed", could anyone please advise what is the right... (2 Replies)
Discussion started by: karthikram
2 Replies

4. Shell Programming and Scripting

Execute command with loop

I have a below command ALTER TABLE `abc` ADD PARTITION ( PARTITION 20130613 VALUES less than (DAY('13'))); Below is requirement It runs in Loop as DAY start with 13 and end with 100 along with this of counter "20130613" also increases per command as the next command should be ... (8 Replies)
Discussion started by: kaushik02018
8 Replies

5. Shell Programming and Scripting

how to use sqlplus command inside for loop

I tried this: for region in 'raj' 'kt' 'kol' 'krl' 'chn' 'dl' 'hr' 'bih' 'ap' do sqlplus -s huw$region/`echo $region`huw#321@huw$region<<! set serveroutput on begin select count(*) from tcd_preferred_cust_201109 end; / exit; done but the error shows like: Syntax error at line 4 :... (1 Reply)
Discussion started by: deepakprasad29@
1 Replies

6. Shell Programming and Scripting

loop to replace txt and add command inside of colon (:)

in one of my script..I have some thing like john:christina : one:: and i want to make it like john:chritina:two:(date command):jackey: basically change 'one' to 'two' and run date :command and add other name: (30 Replies)
Discussion started by: Learnerabc
30 Replies

7. UNIX for Dummies Questions & Answers

For Loop to execute a command on a series of files

Hello, I have a set of input data that I split into batches using the following command: split -l 4000000 MyInput.in Split_a Once I get the split files, I run a certain command on the split files that spews an output. So far I have been doing it without a for loop. How can I translate the... (2 Replies)
Discussion started by: Gussifinknottle
2 Replies

8. Shell Programming and Scripting

C Shell - Command Inside a Loop

I have a command nested in some while loops to parse some data that looks something like this. while ($condition) while ($condition) ... gzcat /dir/$fileName.gz | grep $searchString > out_file end end On the first loop, the command is executed properly (and takes maybe 10... (3 Replies)
Discussion started by: hobbers
3 Replies

9. Shell Programming and Scripting

Execute command inside while problem

Hi! How can I execute a linux command inside the while cicle?? like: This doesn't work. Should I replace the by '' or "" (3 Replies)
Discussion started by: ruben.rodrigues
3 Replies

10. Shell Programming and Scripting

read command (input) inside the while loop

Hi, 'read' command is not working inside the while loop, How can I solve this? Rgds, Sharif. (2 Replies)
Discussion started by: sharif
2 Replies
Login or Register to Ask a Question