assign awk command result to a variable

#!/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.

Remove all the spaces around the equal signs (=) in the assign statements in your script.

Regards

Check if it is empty or not by including

...
echo "${MYSTRING}"
...

If it looks empty but is still not recognized, use maybe something like

...
echo "${MYSTRING}" > tmpfile
...

Then check afterwards with

od -c < tmpfile

if it is really empty.

Also you can try to use following test instead comparing to "":

...
if [[ -z ${MYSTRING} ]]; then
...

And as I just see Franklin's post, insert a blank between the square bracket and the inner characters like:

if [ $MYSTRING = "" ]
    ^              ^
#!/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 :confused::confused:

if [ "${MYSTRING}" = "${TEMP}" ];
                  ^ ^

Need blanks there.

This is correct (compare two string):

#!/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:

#!/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

:b:Good Work:b:
Thanks

Should'nt it be :

if [ "${MYSTRING}" == "${TEMP}" ];
                    ^

No. == is non-standard and only works in some shells; = works in all shells.

This code work good from PackageMaker install.
Add item to the loginwindow.plist for Mac OS X.

#!/bin/sh
#
##
MYSTRING=`awk '/MyApp.app/' /Users/$USER/Library/Preferences/loginwindow.plist`
echo ${MYSTRING}
if [ "${MYSTRING}" = "" ];
then
echo Path not exists

touch ~/Library/Preferences/loginwindow.plist
cp /Users/$USER/Library/Preferences/loginwindow.plist /Users/$USER/Library/Preferences/loginwindow_copy.plist
defaults write ~/Library/Preferences/loginwindow AutoLaunchedApplicationDictionary -array-add '<dict><key>Hide</key><true/><key>Path</key><string>/Applications/MyApp.app</string></dict>'
chmod 777 ~/Library/Preferences/loginwindow.plist 2>/dev/null

else
echo Path exists
fi

:):):slight_smile: