LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices



Reply
 
Search this Thread
Old 09-26-2007, 03:19 AM   #1
culin
Member
 
Registered: Sep 2006
Distribution: Fedora Core 10
Posts: 254

Rep: Reputation: 32
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"
 
Old 09-26-2007, 03:29 AM   #2
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
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}')
 
Old 09-26-2007, 04:25 AM   #3
culin
Member
 
Registered: Sep 2006
Distribution: Fedora Core 10
Posts: 254

Original Poster
Rep: Reputation: 32
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...
 
Old 09-26-2007, 05:54 AM   #4
culin
Member
 
Registered: Sep 2006
Distribution: Fedora Core 10
Posts: 254

Original Poster
Rep: Reputation: 32
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"'
 
Old 09-26-2007, 06:20 AM   #5
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 63
Try:
Code:
if [ "$J" != "$cpu" ] || [ "$J" != "$libs" ]; then
   ...
fi
 
Old 09-26-2007, 06:21 AM   #6
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 63
Or:
Code:
case "$J" in
$cpu|$libs)
   ...
   ;;
esac
 
Old 09-26-2007, 06:32 AM   #7
culin
Member
 
Registered: Sep 2006
Distribution: Fedora Core 10
Posts: 254

Original Poster
Rep: Reputation: 32
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 ??
 
Old 09-26-2007, 07:03 AM   #8
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 63
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.
 
Old 09-27-2007, 01:04 AM   #9
culin
Member
 
Registered: Sep 2006
Distribution: Fedora Core 10
Posts: 254

Original Poster
Rep: Reputation: 32
ya sure matthewg42... thanks...
 
Old 01-24-2013, 01:44 AM   #10
charithsrng
LQ Newbie
 
Registered: Jan 2013
Posts: 19

Rep: Reputation: Disabled
Quote:
Originally Posted by culin View Post
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}'`
 
Old 01-24-2013, 02:33 AM   #11
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.6, Centos 5.10
Posts: 16,324

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


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
compare strings in ksh sharathkv Programming 3 05-01-2007 06:46 AM
Howto compare two strings in C daYz Programming 20 03-04-2007 05:10 AM
shell (bash) scripting - strings vs. integers bullfrog1870 Linux - Newbie 3 11-02-2006 09:51 PM
Shell script to compare blocks of strings? bruno buys Programming 10 04-15-2006 03:16 PM
Shell scripting: exclamation marks in strings Dark_Helmet Programming 9 06-16-2003 07:32 PM


All times are GMT -5. The time now is 05:25 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration