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
# 8  
Old 1 Week Ago
Quote:
Originally Posted by RudiC
What does <path> stand for? Does the readlink result have 7+ / separated fields?
<path> = path to the symbolic link.
-f7 = relates to the version which is 7th field in the above path.

The output of the command is 1.0.1

Last edited by rbatte1; 6 Days Ago at 05:03 AM.. Reason: Added ICODE tags
# 9  
Old 6 Days Ago
By "within a loop" do you happen to mean "behind a pipe?" also known as "inside a subshell"? That will not work. Variables inside a subshell do not get communicated to outside the subshell.
# 10  
Old 6 Days Ago
Quote:
Originally Posted by Corona688
By "within a loop" do you happen to mean "behind a pipe?" also known as "inside a subshell"? That will not work. Variables inside a subshell do not get communicated to outside the subshell.
In which case is there a possibility to evaluate the output of the command to another variable.
eg.

Code:
if ( $(readlink -f <path> | cut -d '/' -f7)" == "$ver"); then


Last edited by rbatte1; 6 Days Ago at 05:04 AM.. Reason: Changed ICODE tags to CODE tags
# 11  
Old 6 Days Ago
Please show us your entire script instead of showing us one line at a time.

It seems highly likely that you are setting the variable you want inside a sub-shell and that makes the assigned value invisible outside of that sub-shell. But we can't know that until we can see all of your code.
These 2 Users Gave Thanks to Don Cragun For This Post:
Neo (6 Days Ago) rbatte1 (6 Days Ago)
# 12  
Old 5 Days Ago
I think the main problem is shown in post#3: the
Code:
readlink -f <path> | cut -d / -f 7

gives an empty string.
(A subshell and/or a wrong assignment would cause additional problems of course.)
If the question is for a better debugging, then I suggest to repeat the readlink -f <path> command without a following pipe. Maybe framed by some "debug" text:
Code:
echo "debug: -->$(readlink -f <path>)<--"

# 13  
Old 5 Days Ago
Quote:
Originally Posted by sankasu
In which case is there a possibility to evaluate the output of the command to another variable.
If your loop is behind a pipe, that would mean stuffing your entire loop inside $( ). Or storing the result in a temporary file.

This is just guessing though. This is a really common problem which we can't rule out without seeing more of your code.

Last edited by Corona688; 5 Days Ago at 03:03 PM..
This User Gave Thanks to Corona688 For This Post:
rbatte1 (4 Days Ago)
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