for loop ( string having spaces )


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting for loop ( string having spaces )
# 1  
Old 05-26-2010
for loop ( string having spaces )

Dear All,

i facing problem to use string having spaces in for loop..


Code:
 file used for FOR LOOP
command.txt
rpm -t -v ttm -D -r RJLL -h YELP
rpm -t -v ttm -D -r RJLL -h ERRT
rpm -t -v ttm -D -r RJLL -h TYYE
rpm -t -v ttm -D -r RJLL -h POOL

Code:
CODE using for execute above command
for i in `cut -c 21- command.txt `
do
cmd=`grep $i command.txt `
echo $cmd
ps_cnt=`ps -ef | grep $i | grep -v grep |wc -l`
if [ $ps_cnt -gt 0 ]
then
   echo "Currently rpm is running for $i"
else
$cmd >> log.txt
fi
done


please help to correct my script..

thanks
# 2  
Old 05-26-2010
try this

Code:
while read line
do
i=`echo $line | cut -c 21- `
cmd=`grep $i command.txt `
echo $cmd
ps_cnt=`ps -ef | grep "${i}" | grep -v grep |wc -l`
if [ $ps_cnt -gt 0 ]
then
   echo "Currently rpm is running for $i"
else
$cmd >> log.txt
fi
done < command.txt

cheers,
Devaraj Takhellambam

Last edited by devtakh; 05-26-2010 at 01:42 AM.. Reason: missed code
# 3  
Old 05-26-2010
Dear Devaraj,

given code is not able to grep proper string( it is spliting )

following error occurred

Code:
sh -x yyy.sh 
+ 0< command.txt
+ read line
+ + echo aaaaa -t -v ttm -D -r RJLL -h YELP
+ cut -c 21-
i=r RJLL -h YELP
+ + grep r RJLL -h YELP command.txt
grep: can't open RJLL
grep: can't open -h
grep: can't open YELP
cmd=command.txt:aaaaa -t -v ttm -D -r RJLL -h YELP
command.txt:aaaaa -t -v ttm -D -r RJLL -h ERRT
command.txt:aaaaa -t -v ttm -D -r RJLL -h TYYE
command.txt:aaaaa -t -v ttm -D -r RJLL -h POOL
+ echo command.txt:aaaaa -t -v ttm -D -r RJLL -h YELP command.txt:aaaaa -t -v ttm -D -r RJLL -h ERRT command.txt:aaaaa -t -v ttm -D -r RJLL -h TYYE command.txt:aaaaa -t -v ttm -D -r RJLL -h POOL
command.txt:aaaaa -t -v ttm -D -r RJLL -h YELP command.txt:aaaaa -t -v ttm -D -r RJLL -h ERRT command.txt:aaaaa -t -v ttm -D -r RJLL -h TYYE command.txt:aaaaa -t -v ttm -D -r RJLL -h POOL
+ + ps -ef
+ wc -l
+ grep r RJLL -h YELP
+ grep -v grep
ps_cnt=0
+ [ 0 -gt 0 ]
+ command.txt:aaaaa -t -v ttm -D -r RJLL -h YELP command.txt:aaaaa -t -v ttm -D -r RJLL -h ERRT command.txt:aaaaa -t -v ttm -D -r RJLL -h TYYE command.txt:aaaaa -

# 4  
Old 05-26-2010
replace
Code:
cmd=`grep $i command.txt `

with

Code:
cmd=`grep "$i" command.txt `


cheers,
Devaraj Takhellambam
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Process files in loop which have spaces in name

I have a folder with files and I have to process them in a loop. However the filenames have space characters, so the list get split. $ touch "File Number_1" $ touch "File Number_2" $ ls "/tmp/File Number"_* /tmp/File Number_1 /tmp/File Number_2 I tried following (sorry for using the... (3 Replies)
Discussion started by: Wernfried
3 Replies

2. Shell Programming and Scripting

Loop through array of arrays of string with spaces

Hi I'm trying to loop through an array that contains other arrays and these arrays consist of strings with spaces. The problem is that I can't seem to preserve the spacing in the string. The string with spaces are either divided into multiple items if I change IFS to \n or all the elements of... (4 Replies)
Discussion started by: kidmanos
4 Replies

3. UNIX for Dummies Questions & Answers

Copying files with spaces in the filename in a for loop

Hi all, I've been tangoing with this one for a couple of days now and I'm still not making any progress. Basically I'm trying to match three numbers in a string from a text file with matching numbers in a jpeg, and then copying the results to another folder. Data looks like this: Model:... (4 Replies)
Discussion started by: faceonline
4 Replies

4. Shell Programming and Scripting

Loop with Find—damn spaces!

Hi Guys, I'm trying to find all files with a particular extension and then loop some actions. The problem is that if the files have spaces in their names I get end up being each word as a separate result rather than the entire file. ext=".txt" out=".rtf" for i in $( find "$1" -name "*$ext" );... (9 Replies)
Discussion started by: imonkey
9 Replies

5. Shell Programming and Scripting

Having a for loop read in lines with spaces?

Is this possible? I have a for loop in a shell script reading a list, but I want each line to be a loop, not each thing with a space. Here is the example: HOSTLIST="\ 1.2.3.4 serverA 1.2.3.5 serverB" for NBUHOST in `echo $HOSTLIST` do ssh ${SERVERNAME} "echo "${NBUHOST}"... (3 Replies)
Discussion started by: LordJezoX
3 Replies

6. Shell Programming and Scripting

problem with for loop and a variable with spaces...Hi

Hi there, I don't understand the following behavior: toto:~$ for word in un "deux trois"; do echo $word; done un deux trois toto:~$ sentence='un "deux trois"' toto:~$ for word in $sentence; do echo $word; done un "deux trois" toto:~$ sentence="un 'deux trois'" toto:~$ for word in... (10 Replies)
Discussion started by: chebarbudo
10 Replies

7. Shell Programming and Scripting

Using sed to replace a string in file with a string in a variable that contains spaces

Hi, i call my shell like: my_shell "my project name" my script: #!/bin/bash -vx projectname=$1 sed s/'PROJECT_NAME ='/'PROJECT_NAME = '$projectname/ <test_config_doxy >temp cp temp test_config_doxy the following error occurres: sed s/'PROJECT_NAME ... (2 Replies)
Discussion started by: vivelafete
2 Replies

8. Shell Programming and Scripting

sed: replace string with another string (with spaces)

Hi I have an XML file with strings XABCD, XEFGHX and XIJKLX. I would like to replace XABCDX with "This is the first string", XEFGHX with "This is the second string" and XIJKLX with "This is the third string". What is the best way to implement this? Should I have a file with the data that is... (4 Replies)
Discussion started by: zmfcat1
4 Replies

9. UNIX for Dummies Questions & Answers

Read a string with leading spaces and find the length of the string

HI In my script, i am reading the input from the user and want to find the length of the string. The input may contain leading spaces. Right now, when leading spaces are there, they are not counted. Kindly help me My script is like below. I am using the ksh. #!/usr/bin/ksh echo... (2 Replies)
Discussion started by: dayamatrix
2 Replies

10. UNIX for Dummies Questions & Answers

For loop using find with file name spaces

Hello All, This question is actually for the service console of VMware ESX 3.5 but is relevant to this forum I think. I have been advised to use the following commands: for i in `find /vmfs/volumes/Test_VMFS/ -name "*.vmx"` do echo "$i" #sed -i 's/scsi1:0.present =... (3 Replies)
Discussion started by: mronsman
3 Replies
Login or Register to Ask a Question