LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Bash script to create bash script (https://www.linuxquestions.org/questions/programming-9/bash-script-to-create-bash-script-583589/)

jag7720 09-10-2007 07:10 AM

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?

colucix 09-10-2007 07:16 AM

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).

jag7720 09-10-2007 07:32 AM

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.

colucix 09-10-2007 07:57 AM

Quote:

Originally Posted by jag7720 (Post 2887426)
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 :)

jag7720 09-10-2007 10:14 AM

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???

colucix 09-10-2007 10:37 AM

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

jag7720 09-10-2007 10:49 AM

Quote:

Originally Posted by colucix (Post 2887572)
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!

colucix 09-10-2007 11:07 AM

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!

jag7720 09-10-2007 11:32 AM

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?

colucix 09-10-2007 11:58 AM

Yeah. Good! ;)

ghostdog74 09-10-2007 07:01 PM

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 10:27 PM.