Assign the result of a multiline command to a variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Assign the result of a multiline command to a variable
# 1  
Old 11-20-2013
Assign the result of a multiline command to a variable

Hi,
I have the following command that lists all the .o files from all the directories except of vwin (which I don't want it)
Code:
for i in `ls -d */*.o|awk '$0 !~ "vwin"'`; do echo $i; done

The result is something like that
Code:
dir1/file1.o
dir1/file2.o
dir2/file3.o
etc.

So, I want to create a variable which will have the following value:
Code:
var1="dir1/file1.o dir1/file2.o dir2/file3.o"

I tried first to do that
Code:
echo `for i in `ls -d */*.o|awk '$0 !~ "vwin"'`; do echo $i; done`

in order to have the multiple lines to one. Unfortunately it doesn't work because of the ";" (I guess).

Can someone help?
Thanks

Last edited by Scrutinizer; 11-21-2013 at 03:27 AM..
# 2  
Old 11-20-2013
That command is extremely redundant. You do not need to cram awk's output into a for-loop to make it print. Leave it out of the loop completely.

ls -d */*.o|awk '$0 !~ "vwin"

It didn't work because you can't nest backticks in that way, it will take the second ` to be the end of the first, not an inner set. For this reason modern shells use $( ) instead of backticks since they nest without trouble. You don't need to nest anything, though.

So your command, with the useless loop removed, boils down to:

Code:
VAR=$(ls -d */*.o|awk '!/vwin/')


Last edited by Corona688; 11-20-2013 at 06:38 PM..
# 3  
Old 11-20-2013
It is more versatile to store the multi-line in VAR.
So you have the option of echo "$VAR"
or on-the-fly folding with (set -f; echo $VAR) or echo "$VAR" | paste -sd " " -
# 4  
Old 11-20-2013
OK then:

Code:
VAR="$(ls -d */*.o|awk '!/vwin/')"

# 5  
Old 11-21-2013
Quote:
Originally Posted by Corona688
OK then:

Code:
VAR="$(ls -d */*.o|awk '!/vwin/')"

The quotes are not useful. Your previous post was already multiline.

Last edited by MadeInGermany; 11-21-2013 at 03:18 AM..
# 6  
Old 11-21-2013
putting a list of files in a string variable assumes the files won't have whitespace in them and this can break things. ideally you'd process them more carefully. if you're using bash, there are arrays and extglob.

Code:
shopt -s extglob
files=( */!(*vwin*).o )

for f in "${files[@]}"; do
   ...
done

# 7  
Old 11-21-2013
Another way

Code:
var=`ls -d */*.o | grep -v vwin`

Now var has the value of all the *.o files

Last edited by Scott; 11-21-2013 at 09:45 AM.. Reason: Added code tags [6th time in 7 posts]
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Assign a command to a variable - Help

Hi, I have the script below. When i assign SSH_COMMAND to "ssh -o ConnectTimeout=2 ${SERVER} ${AS_SUDO} ${COMMANDS}" and then execute it as ${SSH_COMMAND} I get the following error: ssh: Could not resolve hostname sudo: Name or service not known ssh: Could not resolve hostname sudo: Name or... (3 Replies)
Discussion started by: mohca2020
3 Replies

2. UNIX for Beginners Questions & Answers

Need to pass variable in a command and assign value to a variable

Hello All, Hope you're doing well ! I am trying below command to be passed in a shell script, header_date_14 is a variable and $1 is the name of a file I intend to pass as a command line argument, however command line argument is not being accepted. header_date_14=$(m_dump... (8 Replies)
Discussion started by: ektubbe
8 Replies

3. Shell Programming and Scripting

Remove a character and assign result to a variable

I am reading lines from a file that contain a number sign (#) before a three or four digit number: #1043 #677 I can remove the '#' and get just the number. However, I then want to assign that number to a variable and use it as part of a path further on in my program: /mydir/10/1043 for... (5 Replies)
Discussion started by: KathyB148
5 Replies

4. Shell Programming and Scripting

Assign awk gsub result to a variable

Hello, I have searched but failed to find what exactly im looking for, I need to eliminate first "." in a output so i can use something like the following echo "./abc/20141127" | nawk '{gsub("^.","");print}' what i want is to use gsub result later on, how could i achieve it? Let say... (4 Replies)
Discussion started by: EAGL€
4 Replies

5. UNIX for Dummies Questions & Answers

Assign SQL result in shell variable

Hi im trying to assign the result of the db2 command to a variable inside a shell script... : tab_cnt=`db2 "select count(*) from syscat.tables where tabname = 'ABC' and tabschema = 'MATT01'" |head -4|tail +4|cut -c 11` : echo $tab_cnt when i echo im getting a blank value.. im expecting... (1 Reply)
Discussion started by: matt01
1 Replies

6. Shell Programming and Scripting

Assign result to variable

Hi friends, firstly, i can run following expression and i took 100 value. sqlplus -s username/password@TTTEST @umt.sql umt.sql exists "select t.deger from parametre t where t.id=30". result of this query =100 i need to assign this value(100) to variable(for example x... (2 Replies)
Discussion started by: temhem
2 Replies

7. UNIX for Dummies Questions & Answers

Cut Command value assign to variable

Hi, I am new to UNIX Scripting. I have been trying to use the CUT command to retrieve part of the header from a file and assign it to a variable. I have tried searching a lot, but I am still unsuccessful. Sample Header: HJAN BALANCE 20090616 I need to retrieve the date here, which always... (10 Replies)
Discussion started by: ragz_82
10 Replies

8. Shell Programming and Scripting

How to assign the result of a SQL command to more than one variable in shell script.

Hi Friends... Please assist me to assign the result of a SQL query that results two column, to two variables. Pls find the below code that I write for assigning one column to one variable. and please correct if anything wrong.. #! /bin/sh no=' sqlplus -s uname/password@DBname... (4 Replies)
Discussion started by: little_wonder
4 Replies

9. Shell Programming and Scripting

assign awk command result to a variable

#!/bin/sh # ## MYSTRING = `awk '/myApp.app/' /Users/$USER/Library/Preferences/loginwindow.plist` if then echo String not found defaults write /Users/$USER/Library/Preferences/loginwindow AutoLaunchedApplicationDictionary -dict-add -string Hide -bool YES -string Path -string... (9 Replies)
Discussion started by: dedmakar
9 Replies

10. Shell Programming and Scripting

assign subst|grep|sed command result to a variable

Hi, I'm quite new to scripting and I want to modify following line of an existing script: MYVAR=`subst |grep 'L:\\\:' | sed -e 's/.*\\\//'`; What I have to do is to use the content of a variable instead of the constant expression 'L:\\\:' as the grep string to be matched. Assuming I already... (5 Replies)
Discussion started by: snowbiker99
5 Replies
Login or Register to Ask a Question