LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (http://www.linuxquestions.org/questions/linux-general-1/)
-   -   compare strings in shell scripting (http://www.linuxquestions.org/questions/linux-general-1/compare-strings-in-shell-scripting-587418/)

culin 09-26-2007 02:19 AM

compare strings in shell scripting
 
Hi all.....

Code:

#!/bin/sh
echo "Please wait.... The installation is in progress........ "
echo "Searching for the previous installations of scratchbox....."

j="installed"

i= apt-cache show scratchbox-core | grep "Status" | awk '{print $4}'

if [ "$j" == "$i" ]

then
        echo "Scratchbox is already installed on this system"
else
        echo "Scratchbox is not installed in the system......"
        echo "Installing scratchbox.. please wait....."
fi


can anyone tell me what is wrong in the above script...
it is always echoing "Scratchbox is not installed in the system......"
eventhough the output of the command
apt-cache show scratchbox-core | grep "Status" | awk '{print $4}'
is "installed"

colucix 09-26-2007 02:29 AM

Code:

i= apt-cache show scratchbox-core | grep "Status" | awk '{print $4}'
Here you should get a command not found error, since the syntax of the assigment is wrong. The correct syntax for command substitution is
Code:

i=$(apt-cache show scratchbox-core | grep Status | awk '{print $4}')

culin 09-26-2007 03:25 AM

oh..wow.. thanks a lot colucix... :)
and can u please tell me what is the difference between the first one (the one i did ) and the second one (one suggested by u) ???
i am a newbie to scripting...
i will post back if i have some problems in scripting..
thanks again... :)

culin 09-26-2007 04:54 AM

again another problem.. why this logical or inside the shell script is not working... ??? !!!
if [("$j" != "$cpu") -o ("$j" != "$libs")]

it is telling syntax error near unexpected token `"$j"'

matthewg42 09-26-2007 05:20 AM

Try:
Code:

if [ "$J" != "$cpu" ] || [ "$J" != "$libs" ]; then
  ...
fi


matthewg42 09-26-2007 05:21 AM

Or:
Code:

case "$J" in
$cpu|$libs)
  ...
  ;;
esac


culin 09-26-2007 05:32 AM

hey...matthewg42 thanks a lot ... :)
i am really confused here....
why scripting is like this.. just because of spacing it gives me different output and error... ??? !!!! really confused.. is there a standard way of formatting these expressions.. where can i get all these info... ?
and i tried even without the semicolon(;) at the end of the if statement.. it is working without semicolon also... why is like this ??

matthewg42 09-26-2007 06:03 AM

Shell scripting syntax is a little bit clunky compared to other languages, partly because the parser is a single-pass affair, rather than multi-pass parsers used in other languages. But like anything, only you've learned it, it has it's moments.

Perhaps you'd do better to start with a tutorial rather than expecting to be able to implement your script without learning the basics.

culin 09-27-2007 12:04 AM

ya sure matthewg42... thanks... :)

charithsrng 01-24-2013 12:44 AM

Quote:

Originally Posted by culin (Post 2904001)
Hi all.....

Code:

#!/bin/sh
echo "Please wait.... The installation is in progress........ "
echo "Searching for the previous installations of scratchbox....."

j="installed"

i= apt-cache show scratchbox-core | grep "Status" | awk '{print $4}'

if [ "$j" == "$i" ]

then
        echo "Scratchbox is already installed on this system"
else
        echo "Scratchbox is not installed in the system......"
        echo "Installing scratchbox.. please wait....."
fi


can anyone tell me what is wrong in the above script...
it is always echoing "Scratchbox is not installed in the system......"
eventhough the output of the command
apt-cache show scratchbox-core | grep "Status" | awk '{print $4}'
is "installed"

make the changes as below

i=`apt-cache show scratchbox-core | grep "Status" | awk '{print $4}'`

chrism01 01-24-2013 01:33 AM

The original conversation is from 2007(!); please don't bring up old posts like that.


All times are GMT -5. The time now is 12:26 PM.