The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
while loop inside while loop panknil Shell Programming and Scripting 0 01-07-2008 12:49 PM
For loop xramm HP-UX 3 10-10-2007 03:20 PM
While Loop hemangjani Shell Programming and Scripting 2 11-02-2006 11:01 AM
for loop munnabhai1 Shell Programming and Scripting 3 04-06-2006 03:30 PM
how to get the similar function in while loop or for loop trynew Shell Programming and Scripting 3 06-17-2002 12:09 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 04-04-2008
soemac soemac is offline
Registered User
  
 

Join Date: Mar 2008
Posts: 12
cat and loop

Hi I have a simple code that I want to execute.

Code:
out=out.txt
for f in `cat list.txt | head -1`; do
	
	echo $f >> $out
	echo "sleep 5" >> $out
done

cat list.txt | head -1

wget -q -O - 'http://test.com:15100/cgi-bin/search


cat out.txt

wget
sleep 5
-q
sleep 5
-O
sleep 5
-
sleep 5
'http://test.com:15100/cgi-bin/search'
sleep 5

The problem is list.txt contains spaces and they are treated as separator.

My ideal output is of course to show

wget -q -O - 'http://test.com:15100/cgi-bin/search'
sleep 5

Any smart way to do this preferably using less code.
  #2 (permalink)  
Old 04-05-2008
danmero danmero is online now Forum Advisor  
  
 

Join Date: Nov 2007
Location: 45.48-73.63
Posts: 1,438
One awk line can do your job

Code:
 awk '{if($1=="wget")print $0"\nsleept 5"}' list.txt > out.txt

  #3 (permalink)  
Old 04-05-2008
ohagar ohagar is offline
Registered User
  
 

Join Date: Aug 2007
Posts: 16
Not sure why you need a loop

head -1 list.txt > out.txt
echo sleep 5 >> out.txt
  #4 (permalink)  
Old 04-05-2008
cfajohnson's Avatar
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
  
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 2,362
Quote:
Originally Posted by soemac View Post
Any smart way to do this preferably using less code.


Code:
IFS= read -r f < list.txt
printf "%s\n" "$f" > "$out"

  #5 (permalink)  
Old 04-05-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
Just to deviate as little as possible from your original,


Code:
head -1 list.txt >>$out   # yes, the cat is completely meaningless
echo sleep 5 >>$out

If you actually need to get the output into backticks, take care to quote properly.


Code:
string_with_spaces="`head -1 list.txt`"

  #6 (permalink)  
Old 04-05-2008
cfajohnson's Avatar
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
  
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 2,362
Quote:
Originally Posted by era View Post
Just to deviate as little as possible from your original,


Code:
head -1 list.txt >>$out   # yes, the cat is completely meaningless

And head is an unnecessary external command. It is much faster to use read to get the first line from a file.
Quote:
Code:
echo sleep 5 >>$out

If you actually need to get the output into backticks, take care to quote properly.


Code:
string_with_spaces="`head -1 list.txt`"

The quotes are unnecessary.

It is not a string with spaces that is being assigned to the variable, it is the output of command substitution, which is not subject to word splitting by the shell.
  #7 (permalink)  
Old 04-05-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
Granted, the quotes are unnecessary in this context -- I didn't want to go back to the useless for loop just to explain the issue, so I made a rather inexact statement instead. Humor me.

Looks like ohagar already posted an identical suggestion -- sorry, missed that somehow before.

Anyway, I believe there is an option in wget itself to wait a specified number of seconds between retrieving two URLs, so the whole question seems kind of ... intriguing.
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 08:23 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0