LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   How to compare a text value in a shell script and proceed accordingly (http://www.linuxquestions.org/questions/linux-newbie-8/how-to-compare-a-text-value-in-a-shell-script-and-proceed-accordingly-755039/)

hemantsankhla 09-14-2009 05:37 AM

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

i92guboj 09-14-2009 05:50 AM

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.

catkin 09-14-2009 06:12 AM

Quote:

Originally Posted by i92guboj (Post 3682074)
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


lutusp 09-14-2009 06:29 AM

Quote:

Originally Posted by hemantsankhla (Post 3682059)
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


catkin 09-14-2009 07:23 AM

Quote:

Originally Posted by lutusp (Post 3682101)
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

hemantsankhla 09-14-2009 11:50 PM

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

lutusp 09-15-2009 02:17 AM

Quote:

Originally Posted by hemantsankhla (Post 3683061)
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


hemantsankhla 09-18-2009 12:35 AM

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

hemantsankhla 09-18-2009 12:42 AM

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


All times are GMT -5. The time now is 08:22 PM.