How to compare a text value in a shell script and proceed accordingly
Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
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
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.
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
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 02:44 AM..
Reason: Improved grep syntax
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-15-2009 at 12:52 AM..
Reason: in correct 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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.