assign awk command result to a variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting assign awk command result to a variable
# 1  
Old 01-22-2009
Question assign awk command result to a variable

Code:
#!/bin/sh
#
##
MYSTRING = `awk '/myApp.app/' /Users/$USER/Library/Preferences/loginwindow.plist`
if [$MYSTRING = ""]
then
echo String not found
defaults write /Users/$USER/Library/Preferences/loginwindow AutoLaunchedApplicationDictionary -dict-add -string Hide -bool YES -string Path -string '/Applications/myApp.app'
else
echo String found
fi

Always result "String found". What is problem? Thanks.
# 2  
Old 01-22-2009
Remove all the spaces around the equal signs (=) in the assign statements in your script.

Regards

Last edited by Franklin52; 01-22-2009 at 07:47 AM..
# 3  
Old 01-22-2009
Check if it is empty or not by including
Code:
...
echo "${MYSTRING}"
...

If it looks empty but is still not recognized, use maybe something like
Code:
...
echo "${MYSTRING}" > tmpfile
...

Then check afterwards with
Code:
od -c < tmpfile

if it is really empty.

Also you can try to use following test instead comparing to "":
Code:
...
if [[ -z ${MYSTRING} ]]; then
...

# 4  
Old 01-22-2009
And as I just see Franklin's post, insert a blank between the square bracket and the inner characters like:

Code:
if [ $MYSTRING = "" ]
    ^              ^

# 5  
Old 01-22-2009
Code:
#!/bin/sh
#
##
MYSTRING="www"
TEMP="qqq"
echo ${MYSTRING}
echo ${TEMP}
if [ "${MYSTRING}"="${TEMP}" ];
then
echo String equal
else
echo String not equal
fi

Result "String equal". What is not correct? Thanks SmilieSmilie
# 6  
Old 01-22-2009
Code:
if [ "${MYSTRING}" = "${TEMP}" ];
                  ^ ^

Need blanks there.

Last edited by zaxxon; 01-22-2009 at 08:27 AM.. Reason: typo
# 7  
Old 01-22-2009
This is correct (compare two string):
Code:
#!/bin/sh
#
##
MYSTRING="www"
TEMP="qqq"
echo ${MYSTRING}
echo ${TEMP}
if [ "${MYSTRING}" = "${TEMP}" ];
then
echo String equal
else
echo String not equal
fi

First code edit:
Code:
#!/bin/sh
#
##
MYSTRING=`awk '/myApp.app/' /Users/$USER/Library/Preferences/loginwindow.plist`
if [ "${MYSTRING}"  =  "" ];
then
echo String not found
defaults write /Users/$USER/Library/Preferences/loginwindow AutoLaunchedApplicationDictionary -dict-add -string Hide -bool YES -string Path -string '/Applications/myApp.app'
else
echo String found
fi

SmilieGood WorkSmilie
Thanks
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How can I assign awk's variable to shell script's variable?

I have the following script, and I want to assign the output ($10 and $5) from awk to N and L: grdinfo data.grd | awk '{print $10,$5}'| read N L output from gridinfo data.grd is: data.grd 50 100 41 82 -2796 6944 0.016 0.016 3001 2461. where N and L is suppose to be 3001 and 100. I use... (8 Replies)
Discussion started by: geomarine
8 Replies

2. 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

3. 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

4. Shell Programming and Scripting

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) for i in `ls -d */*.o|awk '$0 !~ "vwin"'`; do echo $i; done The result is something like that dir1/file1.o dir1/file2.o dir2/file3.o etc. So, I want to create a... (9 Replies)
Discussion started by: scor6800
9 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 zero to strings that don't appear in block, store result in AWK array

Hi to all, I have this input: <group> <x "2">Group D</x> <x "3">Group B</x> <x "1">Group A</x> </group> <group> <x "1">Group E</x> <x "0">Group B</x> <x "1">Group C</x> </group> <group> ... (11 Replies)
Discussion started by: Ophiuchus
11 Replies

7. Shell Programming and Scripting

assign awk's variable to shell script's variable?

Dear All, we have a command output which looks like : Total 200 queues in 30000 Kbytes and we're going to get "200" and "30000" for further process. currently, i'm using : numA=echo $OUTPUT | awk '{print $2}' numB=echo $OUTPUT | awk '{print $5}' my question is : can I use just one... (4 Replies)
Discussion started by: tiger2000
4 Replies

8. 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

9. 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

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