1

Have to assign variable with command results , I am using set of commands that to be executed in another server and results to be assigned in variable and process from it

The code goes like

 Jump.Sh <username>@[host] > sample.csv << EOF   #will navigate particular box whre following cmd executed
    cd <dirname>
    file=`ls -l <filename> | head -1`   #the file returned ( but not assigned in variable)
    a=`cat $file`                       #getting hanged after cat before the empty value of file
    if [ -s $file ]
    then
       ... code goes
    fi
    EOF

since the file is not getting assigned , i could store the contents in one variable which is to be used in the rest of the code and all other follwing codes are blocked . when examing sample.csv also the result does came

how to assign this variable

2 Answers 2

2

It's not at all clear to me what exactly what you are doing, but you should realize that the line a=$(cat $file) (I've used $() notation rather than backticks to avoid a clash with SO markup and because backticks are a scourge on humanity that should be avoided at all costs) is expanding both $file and the process substitution on the local host. Similarly, the ls -l is listing the directory on the local host from which you run Jump.Sh. You can avoid that by simply quoting the EOF:

Jump.Sh ... > sample.csv << 'EOF'

will prevent any expansion locally and pass the raw text as input to Jump.Sh

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks William . Its work like charm. One more clarification, the thing i am trying to do is to get the contents of file in one varible ( from other server) and use that variable in local . Does the assinging variable will be set at environment ? if so will it be deletd after session overs ?
If you want a variable on the local machine to contain the content of a file on the remote, I would simply do a=$(ssh remote cat /p/a/t/h). (In reality, I would probably copy the file to the local host or access it over a network drive or run a server on the remote host that would allow a client to connect to a socket and get the data, but cat via ssh is not an unreasonable 4th choice.)
now i am got into a new problem, before the Jump.sh line there is variable initialization for filename="sample.txt<date>" which is to searched inside remote server where date is passed by user input which dynamically changes. after i given 'EOF' i can't able to access that filename variable inside this commands. is there any work around for that? banging with errors sorry
1

You wrap the command and the whole heredoc in $( ... ):

x=$(cat <<EOF
1
2
3
EOF
)
echo "$x"
1
2
3

The way you wrote your question forces me to say: the terminating heredoc word (EOF) must appear by itself on a line with no additional whitespace (unless you use <<-EOF, in which case leading tabs (only) are ignored).


Are you aware that this command will give you a lot more than just the filename:

file=`ls -l <filename> | head -1`

That's a strange way to store a filename in a variable. What are you really trying to do there?

3 Comments

If i give like this getting error saying ")" expected and unexpected end of line . i want to get the content of file in a variable where the file name is retrieved from that ls command. is there any way to get it ?
what i am trying is to get the file name store in the variable "actually the ls command with filename will return only one file for sure(only one file is created for particular date) actually we need get the date from local machine to remote machine. based on the date we store the file name in one variable and contents in one variable. and then print those variable by passing into file. I am really struck in getting the local variable to remote if i use 'EOH' and can't assign variable if i use without quotes but command runs in remote only. help needed on this .
i tired with Jump.sh Arg1="0821" "bash -s" <<'EOF' file=$(ls -l sample_$Arg1) echo $file EOF no use , date is not getting substituted and tried with $ and \$ for local and remote variable no use Tried with passing script with parameters no use

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.