Find the answer to your Linux question:
Results 1 to 3 of 3
Hi Could someone tell me why the following doesnt work, please, and how to fix it: Code: #!/bin/bash echo "1|2|3|4|5|6" |{ while read id do stockid=`echo "$id" | mawk 'BEGIN{FS="\|"} ...
  1. #1
    Just Joined!
    Join Date
    Apr 2009
    Posts
    3

    Variable scope and ssh

    Hi

    Could someone tell me why the following doesnt work, please, and how to fix it:

    Code:
    #!/bin/bash
    echo "1|2|3|4|5|6" |{ while read id
    do 
        stockid=`echo "$id" | mawk 'BEGIN{FS="\|"} {gsub("\r",""); print $1}'`; 
        date=`echo "$id" | mawk 'BEGIN{FS="\|"} {gsub("\r",""); print $2}'`; 
        views=`echo "$id" | mawk 'BEGIN{FS="\|"} {gsub("\r",""); print $3}'`;    
     
    done;
    echo "ssh nagiosusr@tf9 'echo $stockid $date $views' " #1
    ssh nagiosusr@tf9 'echo "$stockid $date $views" '#2
    }
    If I paste #1 directly to the console, it works fine (ssh then echos 1 2 3). However, when I run the ssh directly in the script, as in #2? it loses the values for $stockid" "$date" "$views and prints an empty line.

  2. #2
    Just Joined!
    Join Date
    Apr 2009
    Posts
    90
    Simply put, the variables don't exist on the ssh'd machine.

  3. #3
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
    Posts
    8,974
    Most everything inside the single quote 'echo "..." ' will be output as interpreted on the remote system. New scope, new values of variables, which are empty in this case, as per the answer from Touchtecservers.
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

Posting Permissions

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