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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
09-10-2007, 07:10 AM
|
#1
|
Member
Registered: Aug 2003
Posts: 53
Rep:
|
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?
|
|
|
09-10-2007, 07:16 AM
|
#2
|
LQ Guru
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509
|
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.
|
|
|
09-10-2007, 07:32 AM
|
#3
|
Member
Registered: Aug 2003
Posts: 53
Original Poster
Rep:
|
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.
|
|
|
09-10-2007, 07:57 AM
|
#4
|
LQ Guru
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509
|
Quote:
Originally Posted by jag7720
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
|
|
|
09-10-2007, 10:14 AM
|
#5
|
Member
Registered: Aug 2003
Posts: 53
Original Poster
Rep:
|
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???
|
|
|
09-10-2007, 10:37 AM
|
#6
|
LQ Guru
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509
|
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
|
|
|
09-10-2007, 10:49 AM
|
#7
|
Member
Registered: Aug 2003
Posts: 53
Original Poster
Rep:
|
Quote:
Originally Posted by colucix
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!
|
|
|
09-10-2007, 11:07 AM
|
#8
|
LQ Guru
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509
|
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!
|
|
|
09-10-2007, 11:32 AM
|
#9
|
Member
Registered: Aug 2003
Posts: 53
Original Poster
Rep:
|
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?
|
|
|
09-10-2007, 11:58 AM
|
#10
|
LQ Guru
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509
|
Yeah. Good!
|
|
|
09-10-2007, 07:01 PM
|
#11
|
Senior Member
Registered: Aug 2006
Posts: 2,697
|
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.
|
|
|
All times are GMT -5. The time now is 06:32 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|