Sponsored Content
Top Forums UNIX for Beginners Questions & Answers How do I assign the output of a command to a variable within a loop in bash? Post 303033921 by sankasu on Sunday 14th of April 2019 04:02:29 PM
Old 04-14-2019
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; 04-14-2019 at 05:11 PM.. Reason: An additional bracket made it to the code..removed the same
 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

how to assign an output to a variable

Hi, I am giving a grep command, and i am getting the output. i want to store it in a variable for eg a = grep '12345' /dir/1/2/log.txt ( the output is number) b= grep 'basic' /dir/1/2/log1.txt (in this case the output is character) so how to assign the output of grep to a variable ... (1 Reply)
Discussion started by: vasikaran
1 Replies

2. Shell Programming and Scripting

Is it possible to re-assign the variable form the loop?

Good day, everyone! I've got a small problem with re-assigning the variable form the loop. What I'm trying to do is: #!/bin/bash/ VAR1="AAA" VAR2="BBB" VAR3="CCC" for WORD in VAR1 VAR2 VAR3; do $WORD="DDD" echo $WORD done :o That's the output and error messages: -bash:... (2 Replies)
Discussion started by: Nafanja
2 Replies

3. Shell Programming and Scripting

Assign bash command to variable

Hi I am trying to write a function that needs to be able to assign the last run shell command to a variable. The actual command string itself not the exit code of the command. I am using the bash command recall ability to do this as follows: alias pb='ps | grep ash' ... (3 Replies)
Discussion started by: Moxy
3 Replies

4. Shell Programming and Scripting

Assign command (with pipe) output to a variable

Hi , I would like to assign command (with pipe) output to a variable. The code is as follows. The goal of the code is to get the last folder folder with a particular name pattern. myDate=`ls | grep 2009 | tail -1` echo "myDate=" $myDate However, in the presence of the pipe, the code... (3 Replies)
Discussion started by: jeff_cen
3 Replies

5. Shell Programming and Scripting

assign awk output to bash variable

greetings all, I am have a heck of a time trying to accomplish a very simple thing. I have an array of "shortname<spaces>id" created from a dscl output. I want to assign shortname=word1 and id=word2. I have tried shortname=$(${textArray} | awk '{print $1}') - and get 'awk : cannot open... (3 Replies)
Discussion started by: macnetdaemon
3 Replies

6. Shell Programming and Scripting

Re-assign variable's value through which FOR LOOP loops

Hi, I've a requirement where I want to re-assign the value in the variable through which FOR LOOP loops. For e.g. Snippet of code --------------- for i in $var do echo $i >> $tempFile var=`echo $another_var | awk -F" " '{print $1}'` done I am re-assigning var so... (2 Replies)
Discussion started by: dips_ag
2 Replies

7. Shell Programming and Scripting

In bash script, how to assign output of a command to a variable while keeping tabs?

Hi, wondering if it's been asked before but didn't find matches from google. Basically I have this line: myvar=$(echo -e "a\tb") Now somehow the '\t' from the echo output gets replaced with white space and then stored in $myvar. It creates a problem for me later to use tab as delimiter to do... (2 Replies)
Discussion started by: birddie
2 Replies

8. Shell Programming and Scripting

Unable to assign command output to variable

Code set -x STATUS="0" echo $STATUS for i in `ls -ltr Report*|awk '{ print $9 }'` do if then flg = "`head -1 "$i" |cut -c 31-33`" echo `head -1 "$i" |cut -c 31-33` echo $flg if then echo "having Fun" STATUS="2" else echo "no Fun" fi fi (2 Replies)
Discussion started by: Funkeydude
2 Replies

9. Shell Programming and Scripting

Assign variable name through loop

Hi As bash does not support multidimensional arrays (?), I need some help with a problem. What I want to do is to assign variable names containing a counter in a loop . what I want to do is basically something like this: #!/bin/bash for i in {1..8}; do var$i = "some command" done... (6 Replies)
Discussion started by: Tobbev
6 Replies

10. UNIX for Dummies Questions & Answers

Need to get a loop variable from output of a command

Hi, I am trying to get a loop counter i and set its value as the ouput of a command: i=`printmo TEST1 | grep -i TEST2 | wc -l` Then I want to use i as counter to run a loop i number of times. Like if i gets a value of 5 I'll have to run loop 5 times. But will i here be a numeric... (3 Replies)
Discussion started by: pat_pramod
3 Replies
pthread_cond_signal(3T) 												   pthread_cond_signal(3T)

NAME
pthread_cond_signal(), pthread_cond_broadcast() - unblock one or all threads waiting on a condition variable SYNOPSIS
PARAMETERS
cond Pointer to the condition variable to be signaled or broadcast. DESCRIPTION
The function is used to wake-up one of the threads that are waiting for the occurrence of a condition associated with condition variable cond. If there are no threads blocked on cond, this function has no effect. If more than one thread is blocked on cond, the scheduling policy determines which thread is unblocked. It is possible that more than one thread can be unblocked due to a spurious wakeup. The function is used to wake-up all threads that are waiting for the occurrence of a condition associated with the condition variable cond. If there are no threads blocked on cond, this function has no effect. If more than one thread is blocked on cond, the scheduling policy determines the order in which threads are unblocked. The condition variabled denoted by cond must have been dynamically initialized by a call to or statically initialized with the macro An unblocked thread will reacquire the mutex it held when it started the condition wait before returning from or The threads that are unblocked contend for the mutex according to their scheduling policy and priority. The or functions can be called by a thread whether or not it currently owns the condition variable's associated mutex. For predictable scheduling behavior and to prevent lost wake-ups, the mutex should be held when signaling a condition variable. Usage When using condition variables, there is a boolean predicate associated with each condition wait. If this predicate is false, the thread should do a condition wait. Spurious wakeups may occur when waiting on a condition variable. Because the return values from and do not imply anything about the value of this predicate, the predicate should always be re-evaluated. Applications using condition variables typically acquire a mutex and enter a loop which checks the predicate. Depending on the value of the predicate, the thread either breaks out of the loop or waits on the condition. On return from the condition wait, the predicate is re- evaluated. RETURN VALUE
Upon successful completion, and return zero. Otherwise, an error number is returned to indicate the error (the variable is not set). ERRORS
For each of the following conditions, if the condition is detected, the and functions return the corresponding error number: [EINVAL] cond is not a valid condition variable. [EFAULT] cond parameter points to an illegal address. AUTHOR
and were derived from the IEEE POSIX P1003.1c standard. SEE ALSO
pthread_cond_init(3T), pthread_cond_wait(3T). STANDARDS CONFORMANCE
Pthread Library pthread_cond_signal(3T)
All times are GMT -4. The time now is 08:04 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy