LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 09-14-2009, 05:37 AM   #1
hemantsankhla
LQ Newbie
 
Registered: Sep 2009
Posts: 5

Rep: Reputation: 0
How to compare a text value in a shell script and proceed accordingly


Hi,

I am triying to develop a shell script
which has to read a variable from a text file is the variable is yes then it should proceed otherwise it should stop

for eg

my text file name is read.txt

contents is
build= yes/no

# so if i i say want that if build is yes then it should proceed


Please help!!
 
Old 09-14-2009, 05:50 AM   #2
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,037

Rep: Reputation: 373Reputation: 373Reputation: 373Reputation: 373
You could use grep to extract the value, bash is not too good at this kind of thing but well,

Code:
VAR=$(grep 'build=' read.txt)
You should as well using head or tail so you know that only one line will be read, otherwise your program will fail if there's more than one line matching 'build=' in your read.txt file.

Code:
VAR=$(grep 'build=' read.txt | head -n1)
Then you can strip the 'build=' substring this way:

Code:
VAR=${VAR#build=}
Then it's a matter of doing the checks with if, case, or whatever you prefer.

Last edited by i92guboj; 09-14-2009 at 05:53 AM.
 
Old 09-14-2009, 06:12 AM   #3
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,541
Blog Entries: 28

Rep: Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176
Quote:
Originally Posted by i92guboj View Post
Code:
VAR=$(grep 'build=' read.txt | head -n1)
VAR=${VAR#build=}
Safer to double quote the first assignment in case the line contains tabs or spaces. You may need to strip leading and trailing spaces from the value. Leading to ...
Code:
shopt -s extglob # allow extended pattern matching operators
VAR="$(grep 'build=' read.txt | head -n1)"
VAR=${VAR##build=*( )}  # Omitting double quotes here strips trailing spaces but will give an error if there is more than one space-separated word after build=
Alternatively you could write your configuration file as shell script and source it using the "." command
Code:
c@CW8:~$ cat my.cfg
# some comment
build=yes
foo=bar
c@CW8:~$ . my.cfg
c@CW8:~$ echo $build $foo
yes bar
 
Old 09-14-2009, 06:29 AM   #4
lutusp
Member
 
Registered: Sep 2009
Distribution: Fedora
Posts: 835

Rep: Reputation: 101Reputation: 101
Quote:
Originally Posted by hemantsankhla View Post
Hi,

I am triying to develop a shell script
which has to read a variable from a text file is the variable is yes then it should proceed otherwise it should stop

for eg

my text file name is read.txt

contents is
build= yes/no

# so if i i say want that if build is yes then it should proceed


Please help!!
Try this:

Code:
if grep -Pq "build\s*=\s*yes" read.txt
then
   # actions on success
else
   # actions on failure
fi

Last edited by lutusp; 09-15-2009 at 01:44 AM. Reason: Improved grep syntax
 
Old 09-14-2009, 07:23 AM   #5
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,541
Blog Entries: 28

Rep: Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176
Quote:
Originally Posted by lutusp View Post
Try this:

Code:
if grep -Pq "build\s+=\s+yes" read.txt
then
   # actions on success
else
   # actions on failure
fi
That's neat. How many more ways can we skin this cat?
Code:
#!/bin/bash
shopt -s extglob
case "$( /bin/cat my.cfg )" in  
    *'  
build='*( )'yes'*( )'
'* )
        echo 'build: yes'
        ;;
    * ) 
        echo 'build: not yes'
esac
Ugly but it works
 
Old 09-14-2009, 11:50 PM   #6
hemantsankhla
LQ Newbie
 
Registered: Sep 2009
Posts: 5

Original Poster
Rep: Reputation: 0
thank you for your response
but
Sorry i forgot to tell i have to develop this script is csh


#! /bin/csh -f

setenv DISPLAY 192.23.9.69:1
set file="$(find /rubyscript/read.txt)"
echo $file
cat $file
set var1=(grep 'build=no' read.txt)
echo $var1
set var2="build=no"
echo $var2
if ($var1 != $var2) then
command not to build the script.
else
comand to build the script

endif

i was trying something like this but i think there is some problem with the syntex of variable declaration

Last edited by hemantsankhla; 09-14-2009 at 11:52 PM. Reason: in correct post
 
Old 09-15-2009, 02:17 AM   #7
lutusp
Member
 
Registered: Sep 2009
Distribution: Fedora
Posts: 835

Rep: Reputation: 101Reputation: 101
Quote:
Originally Posted by hemantsankhla View Post
thank you for your response
but
Sorry i forgot to tell i have to develop this script is csh


#! /bin/csh -f

setenv DISPLAY 192.23.9.69:1
set file="$(find /rubyscript/read.txt)"
echo $file
cat $file
set var1=(grep 'build=no' read.txt)
echo $var1
set var2="build=no"
echo $var2
if ($var1 != $var2) then
command not to build the script.
else
comand to build the script

endif

i was trying something like this but i think there is some problem with the syntex of variable declaration
I don't use csh, but the following may give you some ideas. One can use grep from within csh, so not much is changed:

Code:
set outcome = `grep -P "\bbuild\s*=\s*yes\b" /rubyscript/read.txt`

if ( $#outcome >= 1 ) then
   echo "Build outcome here."
else
   echo "Don't build outcome here."
endif
 
Old 09-18-2009, 12:35 AM   #8
hemantsankhla
LQ Newbie
 
Registered: Sep 2009
Posts: 5

Original Poster
Rep: Reputation: 0
#!/bin/csh -f
#set numlinks1 = `cat pri.txt | wc -l`
set REPLY = `grep "yes" pri.txt`
echo reply = $REPLY

if($REPLY == "yes") then
echo This is a yes
else if($REPLY = "no") then
echo This is a no
endif
endif


Thanks every one i got it working.
 
Old 09-18-2009, 12:42 AM   #9
hemantsankhla
LQ Newbie
 
Registered: Sep 2009
Posts: 5

Original Poster
Rep: Reputation: 0
#! /bin/csh - f

set var1 = "buildyes"
echo $var1

set var2 = `cat /rubyscript/read.txt `
echo $var2

switch ($var2)
case buildno:
echo no
breaksw
case buildyes:
echo yes
breaksw
endsw



even this worked fine for the csh
 
  


Reply

Tags
compare, csh, cshell, script, shell, syntax, text, variable, variables


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 Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell Script Compare Folders corteplaneta Programming 8 09-18-2010 01:32 AM
How to make shell script wait for key press to proceed... ddenton Linux - General 13 12-02-2008 04:25 AM
Script to compare numbers inside two text files bugg_deccan Programming 3 10-17-2007 09:53 PM
shell script: compare 2 files anhtt Programming 6 08-29-2007 02:39 AM
Need text pattern compare script kscott121 Linux - Software 4 05-10-2004 01:13 PM


All times are GMT -5. The time now is 01:23 AM.

Main Menu
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