LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 09-10-2007, 07:10 AM   #1
jag7720
Member
 
Registered: Aug 2003
Posts: 50

Rep: Reputation: 15
Bash script to create bash script


I have a csv that contains 3 columns and 65 rows. I need to make a script from each row.

Here is a sample of the scv

filename_1.ga10 monthend/foo/bar/ 192.168.1.100:bar/

Here is my code

Code:
#!/bin/bash

i="0"
while [ $i -lt 65 ]
for x in `cat gsmd.csv  | awk -F , {'print $1'}`
do
cat gsmd.csv | awk -F , {'print "#!/bin/sh\n#GETTING FILE\nscp root@192.168.1.150:/data/"$2 $1 $2"\n#PUTTING FILE\nscp "$2 $1" root@"$3'} > $x.sh
i=$[$i+1]
done
When I run this

sh -x shell.sh

I get the following

jgreene@jgreene-desktop:$ sh -x shell.sh
+ i=0
shell.sh: line 11: syntax error: unexpected end of file


Can anyone tell me what I am doing wrong?
 
Old 09-10-2007, 07:16 AM   #2
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,368

Rep: Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910
The while loop is not complete. The do and done statements refer to the for loop. Hence the "unexpected end of file" error. By the way, I would check the logic more carefully: probably you don't need the inner for loop (or the outer while loop) if you want to parse the entire file one row at a time.

Edit: you may also consider to do a simple and pure awk code, using redirection inside awk (same syntax as shell redirection).

Last edited by colucix; 09-10-2007 at 07:25 AM.
 
Old 09-10-2007, 07:32 AM   #3
jag7720
Member
 
Registered: Aug 2003
Posts: 50

Original Poster
Rep: Reputation: 15
Thanks for the quick reply.

If I run the awk code on its own I get a single script with all 65 actions in it instead of 65 scripts.

I'm very new at scripting. I understand what you wrote but I don't know what to do about it.
 
Old 09-10-2007, 07:57 AM   #4
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,368

Rep: Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910
Quote:
Originally Posted by jag7720 View Post
If I run the awk code on its own I get a single script with all 65 actions in it instead of 65 scripts.
Not exactly, if you use redirection inside awk! For example something like this:
Code:
gawk '{print something > $1".sh"}' somefile
You may check The GNU Awk User's Guide, chapter 4.6 for details
 
Old 09-10-2007, 10:14 AM   #5
jag7720
Member
 
Registered: Aug 2003
Posts: 50

Original Poster
Rep: Reputation: 15
Ok, so this is what I have so far...



Code:
#!/bin/sh

INFILE=gsmd.csv

while read curline; do

i=`expr $i + 1`

awk -F , {'print "#!/bin/sh\n#GETTInG FILE\nscp root@192.168.1.150:/data/"$2 $1 $2"\n#PUTTING FILE\nscp "$2 $1" root@"$3'} > echo $curline | awk -F , {'print $1'}

echo $curline

done < $INFILE



only problem now is that I can't get the script to name the output file from $1 in the second awk statement.

it tells me

awk: cannot open par410p1.ga08,monthend/gsa/rco/,192.168.1.100:rco/ (No such file or directory)


thoughts???
 
Old 09-10-2007, 10:37 AM   #6
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,368

Rep: Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910
Well... this is not exactly what I meant. Awk parses the text file, executing all commands on each line, so maybe there is no need to loop over the file with shell commands (if I correctly understand your question). So, why not simply...?
Code:
awk -F, '{print "#!/bin/sh\n#GETTInG FILE\nscp root@192.168.1.150:/data/"$2 $1 $2"\n#PUTTING FILE\nscp "$2 $1" root@"$3 > $1".sh"}' gsmd.csv
 
Old 09-10-2007, 10:49 AM   #7
jag7720
Member
 
Registered: Aug 2003
Posts: 50

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by colucix View Post
Well... this is not exactly what I meant. Awk parses the text file, executing all commands on each line, so maybe there is no need to loop over the file with shell commands (if I correctly understand your question). So, why not simply...?
Code:
awk -F, '{print "#!/bin/sh\n#GETTInG FILE\nscp root@192.168.1.150:/data/"$2 $1 $2"\n#PUTTING FILE\nscp "$2 $1" root@"$3 > $1".sh"}' gsmd.csv
Holy one liners Batman

You gotta be kidding... that is exactly what I was looking for.

You da bess....

Thanks!
 
Old 09-10-2007, 11:07 AM   #8
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,368

Rep: Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910
You're welcome! I hope it was clear the whole frame and the meaning of the errors you got before reaching the solution. Maybe, you will check again the syntax of your awk command, since it looks like the obtained scripts will not work as you expect, he he he... Cheers!
 
Old 09-10-2007, 11:32 AM   #9
jag7720
Member
 
Registered: Aug 2003
Posts: 50

Original Poster
Rep: Reputation: 15
It was sort of clear. I understand (sort of) why it didn't work but I had no idea how to resolve it


You are right... that syntax won't work

but this

#!/bin/sh

awk -F, '{print "#!/bin/sh\n#GETTING FILE\nscp root@192.168.1.150:/data/"$2 $1" "$2 $1"\n#PUTTING FILE\nscp "$2 $1" root@"$3 > $1".sh"}' gsmd.csv


gives me this

#!/bin/sh
#GETTING FILE
scp root@192.168.1.150:/data/monthend/foo/r11/par410p1.ga00 monthend/foo/r11/par410p1.ga00
#PUTTING FILE
scp monthend/foo/r11/par410p1.ga00 root@159.142.175.59:r11/
#!/bin/sh
#GETTING FILE
scp root@192.168.1.150:/data/monthend/gsa/rco/par410p1.ga00 monthend/foo/rco/par410p1.ga00
#PUTTING FILE
scp monthend/foo/rco/par410p1.ga00 root@159.142.175.59:rco/par410p1.ga00

and that should work, No?
 
Old 09-10-2007, 11:58 AM   #10
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,368

Rep: Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910
Yeah. Good!
 
Old 09-10-2007, 07:01 PM   #11
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 239Reputation: 239Reputation: 239
from what i see, you can just do everything inside one script..there's no need to create another script just contain your scp commands.
 
  


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


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to create a bash script to count? Alinuxnoob Programming 4 01-28-2007 10:48 PM
create a bash script that restarts? nny0000 Linux - Software 7 09-15-2006 05:14 PM
Cannot create folders with bash script called from php keyF Linux - Software 4 06-25-2006 10:58 AM
How to create a bash script to automatically disown a process. jon_k Linux - Software 5 06-19-2005 05:53 AM
How do I create application launcher using bash script msgclb Programming 2 01-30-2005 06:28 AM


All times are GMT -5. The time now is 07:30 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration