How do I assign the output of a command to a variable within a loop in bash?


Login or Register to Reply

 
Thread Tools Search this Thread
# 1  
Old 1 Week Ago
How do I assign the output of a command to a variable within a loop in bash?

In the else of the main if condition .

Code:
else
       set lnk = $(readlink -f <path> | cut -d '/' -f7)
        echo "$lnk" 
        if [[ "$lnk" == "$ver" ]]

When I run the above on command line , the execution seems to be fine and I get the desired output. But when I try to assign it to a variable within a loop , it doesnt get assigned.Hence the "if condition" after the variable assignment doesnt get evaluated.

Os Version : RHEL 6.0

Last edited by sankasu; 1 Week Ago at 04:11 PM.. Reason: An additional bracket made it to the code..removed the same
# 2  
Old 1 Week Ago
Run the script with set -x and see what comes out.
# 3  
Old 1 Week Ago
The output suggests the same thing which I mentioned in my problem statement.

Code:
++ readlink -f <host>:<sym lnk>
++ cut -d / -f7
+ set lnk =
+ echo

Doesnt seem to be assigning anything in the loop to the variable lnk.

Last edited by rbatte1; 5 Days Ago at 05:01 AM.. Reason: Added CODE tags
# 4  
Old 1 Week Ago
Quote:
Originally Posted by sankasu
... When I run the above on command line , the execution seems to be fine and I get the desired output. ...
A bit difficult to believe. In bash, set is not used to assign variables, be it interactively or in a script. Simply drop it. And, remove the spaces around the = sign.
# 5  
Old 1 Week Ago
Code:
lnk="$(readlink -f <path> | cut -d '/' -f7)"

# 6  
Old 1 Week Ago
Thanks admins.. I had actually tried that but still no luck .

Code:
lnk="$(readlink -f <path> | cut -d '/' -f7)"
echo "$lnk"

Output

Code:
++ readlink -f <path>
++ cut -d / -f7
+ lnk=
+ echo ''

--- Post updated at 01:13 PM ---

Quote:
Originally Posted by RudiC
A bit difficult to believe. In bash, set is not used to assign variables, be it interactively or in a script. Simply drop it. And, remove the spaces around the = sign.
I actually meant ran the below on command line to get the desired output.

readlink -f <path> | cut -d '/' -f7

Last edited by rbatte1; 5 Days Ago at 05:02 AM.. Reason: Changed ICODE tags to CODE tags and added more CODE tags for output.
# 7  
Old 1 Week Ago
What does <path> stand for? Does the readlink result have 7+ / separated fields?
Login or Register to Reply

|
Thread Tools Search this Thread
Search this Thread:
Advanced Search

More UNIX and Linux Forum Topics You Might Find Helpful
Assign Values to a Variable in While Loop and Update the File infernalhell Shell Programming and Scripting 7 05-15-2018 11:06 AM
Do While Loop + Read From File + assign line to a variable ParthThakkar Shell Programming and Scripting 1 03-10-2015 09:53 AM
Need to get a loop variable from output of a command pat_pramod UNIX for Dummies Questions & Answers 3 10-25-2012 02:28 PM
Assign variable name through loop Tobbev Shell Programming and Scripting 6 10-02-2012 06:07 AM
Assign output to dynamic variable ganga.dharan Shell Programming and Scripting 6 09-26-2012 04:02 AM
Unable to assign command output to variable Funkeydude Shell Programming and Scripting 2 05-22-2012 10:15 AM
In bash script, how to assign output of a command to a variable while keeping tabs? birddie Shell Programming and Scripting 2 06-27-2011 03:11 PM
Bash assign string to variable lio123 Shell Programming and Scripting 25 05-02-2011 03:56 PM
Re-assign variable's value through which FOR LOOP loops dips_ag Shell Programming and Scripting 2 01-19-2011 04:56 AM
assign awk output to bash variable macnetdaemon Shell Programming and Scripting 3 11-24-2009 01:44 PM
Assign command (with pipe) output to a variable jeff_cen Shell Programming and Scripting 3 11-19-2009 08:41 AM
Assign bash command to variable Moxy Shell Programming and Scripting 3 04-29-2009 03:52 AM
Is it possible to re-assign the variable form the loop? Nafanja Shell Programming and Scripting 2 12-09-2007 09:30 PM
hot to assign output to a variable walnut Shell Programming and Scripting 1 02-22-2006 08:19 PM
how to assign an output to a variable vasikaran UNIX for Dummies Questions & Answers 1 07-05-2005 05:22 AM