LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
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, 02: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, 02:29 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
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, 03: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, 04: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, 05:20 AM   #5
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

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

Rep: Reputation: 65
Or:
Code:
case "$J" in
$cpu|$libs)
   ...
   ;;
esac
 
Old 09-26-2007, 05: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, 06:03 AM   #8
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
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, 12: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, 12:44 AM   #10
charithsrng
LQ Newbie
 
Registered: Jan 2013
Posts: 26

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, 01:33 AM   #11
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

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


Reply



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 05:46 AM
Howto compare two strings in C daYz Programming 20 03-04-2007 04:10 AM
shell (bash) scripting - strings vs. integers bullfrog1870 Linux - Newbie 3 11-02-2006 08:51 PM
Shell script to compare blocks of strings? bruno buys Programming 10 04-15-2006 02:16 PM
Shell scripting: exclamation marks in strings Dark_Helmet Programming 9 06-16-2003 06:32 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 08:41 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
Open Source Consulting | Domain Registration