Find the answer to your Linux question:
Results 1 to 6 of 6
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/ ...
  1. #1
    Just Joined!
    Join Date
    Sep 2007
    Posts
    4

    Bash script to create bash scripts

    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?

  2. #2
    Linux User
    Join Date
    Jun 2007
    Posts
    318
    You need the do/done sequence for the while statement:

    Code:
    #!/bin/bash -vx
    
    i="0"
    
    while [ $i -lt 65 ]
    do
    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.15
    0:/data/"$2 $1 $2"\n#PUTTING FILE\nscp "$2 $1" root@"$3'} > $x.sh
    i=$[$i+1]
    done
    done

  3. #3
    Banned
    Join Date
    Sep 2007
    Location
    Wilmer Texas. It's near Dallas. Visit Deep Ellum some time.
    Posts
    17
    Quote Originally Posted by vsemaska View Post
    You need the do/done sequence for the while statement:

    Code:
    #!/bin/bash -vx
    
    i="0"
    
    while [ $i -lt 65 ]
    do
    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.15
    0:/data/"$2 $1 $2"\n#PUTTING FILE\nscp "$2 $1" root@"$3'} > $x.sh
    i=$[$i+1]
    done
    done


    I just tried your code. This is what I got:

    csv: gsmd.{I forgot}: no such file or directory.

    It started repeatind like a bottle topper in a beer factory.

    Any ideas ??

    The issue is that no ones code works {usually at all} correctly in Dreamlinux 2.2 MM.

    I first must get into the building before I get a standing ovation. Right now I'm standing outside holding a cardboard sign.

    I'm lost....

  4. #4
    Linux User
    Join Date
    Jun 2007
    Posts
    318
    Are you in the directory where gsmd.csv is located? It sounds like you're in the wrong directory.

    The reason it kept repeating is because the while loop is now working.

    I added the '-vx' option on the 1st line to display the code as it's being executed. If it isn't that you were in the wrong directory post about 10 lines of the output that includes the error.

  5. #5
    Just Joined!
    Join Date
    Sep 2007
    Posts
    4
    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???

  6. #6
    Just Joined!
    Join Date
    Sep 2007
    Posts
    4

    [solved]

    This is what I found on another forum that works

    #!/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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...