How i can put the result of a command inside a bash variable?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How i can put the result of a command inside a bash variable?
# 1  
Old 02-13-2012
How i can put the result of a command inside a bash variable?

Code:
#!/bin/bash
#...
for i in `ls -c1 /usr/share/applications`
do
name="cat $i | grep ^Name= | cut -d = -f2"
echo $name
#...
done

Now inside name as output is present:
Quote:
cat arandr.desktop | grep ^Name= | cut -d = -f2
...
while i want only the result of the command.

Ideally i would like obtain that information using only bash ... or bash+sed or bash+awk but i don't known if is possible so, for the moment, i used bash+grep+cut.

The final project will be an automatic menu generator for pekwm and, i hope, even for icewm.

If is possible i would like obtain that information using only the language that i specify above because i don't want use big languages (i means big package as perl, python, etc).
The final stage will be a test even on openbsd.

Note:
This is the second day that i'm broken my head on the wall. Smilie
# 2  
Old 02-13-2012
Code:
name=`cat $i | grep ^Name= | cut -d = -f2`

This User Gave Thanks to bartus11 For This Post:
# 3  
Old 02-13-2012
If i use ' i obtain that output:
Code:
cat $i | grep ^Name= | cut -d = -f2

instead the result of the command.
Don't work ... Smilie

Last edited by Franklin52; 02-14-2012 at 04:18 AM.. Reason: Please use code tags for code and data samples, thank you
# 4  
Old 02-13-2012
Those are not single quotes but backquotes, also called a grave accent mark. Usually to the left of the number 1 key. You can also do this which handles nesting commands better:
Code:
name=$(cat $i | grep ^Name= | cut -d = -f2)

Actually, save a process and useless use of cat:
Code:
name=$(grep ^Name=  $i | cut -d = -f2)


Last edited by gary_w; 02-13-2012 at 01:47 PM..
This User Gave Thanks to gary_w For This Post:
# 5  
Old 02-13-2012
Ok, now i tried using backquotes.
But i obtain errors instead of the result:

$ ./prova.sh
Code:
cat: arandr.desktop: File o directory non esistente

cat: qv4l2.desktop: File o directory non esistente

...

Naturally that files are present.
# 6  
Old 02-13-2012
The `ls ...` command substitution in the for loop will mangle any filenames containing IFS characters (by default, space, tab, and newline). Perhaps that's related to your breakage.

Regards,
Alister
# 7  
Old 02-13-2012
Code:
#!/bin/bash

for i in `ls -c1 /usr/share/applications`
do
name=`cat /usr/share/applications/$i | grep ^Name= | cut -d = -f2`
echo $name
done

Ok thanks i resolved there was another error inside the script. Smilie

---------- Post updated at 06:00 PM ---------- Previous update was at 05:52 PM ----------

Thanks for the suggestions:
Quote:
Originally Posted by gary_w
Code:
name=$(grep ^Name=  $i | cut -d = -f2)

but for categories i use a more complex solution:

Code:
categories=`cat /usr/share/applications/$i | grep ^Categories= | sed 's/;/=/g' | cut -d = -f2`

work
if i change on the other way don't work.

Last edited by Franklin52; 02-14-2012 at 04:18 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to run several bash commands put in bash command line?

How to run several bash commands put in bash command line without needing and requiring a script file. Because I'm actually a windows guy and new here so for illustration is sort of : $ bash "echo ${PATH} & echo have a nice day!" will do output, for example:... (4 Replies)
Discussion started by: abdulbadii
4 Replies

2. Shell Programming and Scripting

Put a command into router through sshpass bash script

hello, i am facing some issue. I am using a simple bash script that via sshpass put a command into router. Now, problem is that i have a file and commands into it. sshpass -p $pass ssh -o $log -n $user@$h /ip address set address=10.0.0.1/24 so if I have that command ip address set ... (0 Replies)
Discussion started by: tomislav91
0 Replies

3. Shell Programming and Scripting

Bash to store result in variable for other lines in script to use

I can not figure out how to capture the $filename variable store by the bash. #!/bin/bash # oldest folder stored as variable for analysis, version log created, and quality indicators matched to run dir=/home/cmccabe/Desktop/NGS/test find "$dir" -maxdepth 1 -mindepth 1 -type d -printf... (5 Replies)
Discussion started by: cmccabe
5 Replies

4. Shell Programming and Scripting

CUT command not giving correct result inside loop

Hi, i have a source file and have 3 columns and separated by "|" .i want to split this 3 columns in different variable.When i am executing this values indivisually giving correct result but when the same execute inside a for loop,it's giving issues. Src file(jjj.txt) -------... (8 Replies)
Discussion started by: raju2016
8 Replies

5. Shell Programming and Scripting

Shell script variable $1 used with put command

I have the following script used, i am new to shell scripting. tryign to understand. in the put $BASE_FOLDER/$base_name holds which path. What does it mean by $1 second path in put command is it constructing this path: /user/hive/warehouse/stage.db/$1 what is $1 holding in above path. ... (2 Replies)
Discussion started by: cplusplus1
2 Replies

6. UNIX for Dummies Questions & Answers

How to get all the files inside a folder then put the value in a variable?

Hi here is my code for i in `ls *.cmd` do msg aaa imp -U$AAAUSER -P$AAAUSERPWD <$i>>$curdir/import_tap.out -Jutf8 done I can only get all files with .cmd extension. what i need to get are all the files inside the specific folder Thanks (2 Replies)
Discussion started by: cmarzan
2 Replies

7. UNIX for Dummies Questions & Answers

NEWB Question - BASH COMMAND RESULT for ${0##*/}

This should be extremely simple and someone will probably answer it in .5 seconds. I need to know what: VARIABLE=${0##*/} does? I do not have a shell handy to just try it in. I am reading through some scripts and need to understand this line. Any help is appreciated. Many thanks! -... (3 Replies)
Discussion started by: chrisgoetz
3 Replies

8. Windows & DOS: Issues & Discussions

Read command result as variable

Hi all, Is there a simple way to read a command output as a variable? I've running a batch file to do: attrib.exe * | find /c /v "" >filecount.txt but rather than write it to the txt file I'd like to read the total in as a variable to pass to the rest of the bat file... Any help much... (1 Reply)
Discussion started by: Grueben
1 Replies

9. Shell Programming and Scripting

Not able to store command inside a shell variable, and run the variable

Hi, I am trying to do the following thing var='date' $var Above command substitutes date for and in turn runs the date command and i am getting the todays date value. I am trying to do the same thing as following, but facing some problems, unique_host_pro="sed -e ' /#/d'... (3 Replies)
Discussion started by: gvinayagam
3 Replies

10. Shell Programming and Scripting

How to put output of one command into a variable

Hi, Let say I have these 3 files (state, list and myscript). I want to be able get the sample output like below when I run myscript. Any one know how to fix the code? TIA. ~~~~~~~~~~~~~~~ > cat /home/state CA > cat /home/list CA 100 50 20 AUS 120 61 10 > cat myscript... (6 Replies)
Discussion started by: joker_789us
6 Replies
Login or Register to Ask a Question