check if a variable contains a string


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users check if a variable contains a string
# 1  
Old 12-11-2008
check if a variable contains a string

hi

I have an if condition that states:

if [$x does not contain $y]; then
exit


how to translate this?

$x is a path
$y is a string that comes at the end of the path

thx
# 2  
Old 12-11-2008
one way
Code:
echo "$x" | grep -q "$y" 
if [[ $? -ne 0 ]] ; then
   exit
fi

# 3  
Old 12-11-2008
If "$y" always comes at the end of "$x" and (you said the content is a path) is preceeded by a slash ("/") you could use the shells variable expansion feature to trim "$x" accordingly and compare if it equals to "$y". "${x##*/}" suppresses every content of "$x" up to the last slash:

Code:
x="/a/b/c/d/e"
y="f"                   # comment this, uncomment the following line and run again
# y="e"                 

if [ "${x##*/}" = "$y" ] ; then
     echo "x and y are equal"
else
     echo "x and y are different"
fi

Note that "equal" means just "equal in the mentioned regard".

i hope this helps.

bakunin
# 4  
Old 01-10-2009
hi

the script works fine if x="/a/b/c/d/e"

but the script does not work if x="/a/b/c/d/e/"?

i mean the last slash is after the last string.

any idea?

thanks
# 5  
Old 01-11-2009
If you use bash 3.0 or greater try
Code:
if [[ $var =~ "*/$" ]] ; then
   var=$( dirname $var)
fi

else try
Code:
echo "$var" | grep -q '*/$'
if [[ $? -eq 0 ] ; then
   var=$(dirname $var)
fi

# 6  
Old 01-12-2009
is there a better solution that gets the last string and removes the trailing slash if it exists?

thanks.
# 7  
Old 01-12-2009
Hammer & Screwdriver Here is another approach

Code:
> echo $sample
/a/b/c/d/e
> echo $sample1
/a/b/c/d/e/

> echo $sample | sed "s/\/$//" | awk -F"/" '{print $NF}'
e
> echo $sample1 | sed "s/\/$//" | awk -F"/" '{print $NF}'
e

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Check if string variable is a subset of another string variable

Below is my ksh shell script where I need to check if variable fileprops is a subset of $1 argument. echo "FILE PROPERTY: $fileprops" echo "PARAMETER3: $1" if ; then echo "We are Good. $line FILE is found to be INTACT !! " else echo... (2 Replies)
Discussion started by: mohtashims
2 Replies

2. Shell Programming and Scripting

Check file for string existence before appending it with string

I want to append file with a string but before doing that i want to check if this string already exist in that file.I tried with grep on Solaris 10 but unsuccessful.Man pages from grep seems to suggest if the string is found command status will be 0 and if not 1.But i am not finding it.May be i... (2 Replies)
Discussion started by: sahil_shine
2 Replies

3. Shell Programming and Scripting

korn shell: check the content of a string of a variable

hello, i have a variable which should have following content : var="value1" or var="value2" or var="value2:*" # example: value2:22 how can i check : - if the content is ok (value1 / value2* ) - the two options of "value2" when content is example "value2:22" , i want to split... (3 Replies)
Discussion started by: bora99
3 Replies

4. UNIX for Dummies Questions & Answers

Comparing a String variable with a string literal in a Debian shell script

Hi All, I am trying to to compare a string variable with a string literal inside a loop but keep getting the ./testifstructure.sh: line 6: #!/bin/sh BOOK_LIST="BOOK1 BOOK2" for BOOK in ${BOOK_LIST} do if then echo '1' else echo '2' fi done Please use next... (1 Reply)
Discussion started by: daveu7
1 Replies

5. Shell Programming and Scripting

replace (sed?) a string in file with multiple lines (string) from variable

Can someone tell me how I can do this? e.g: a=$(echo -e wert trewt ertert ertert ertert erttert erterte rterter tertertert ert) How do i replace the STRING with $a? I try this: sed -i 's/STRING/'"$a"'/g' filename.ext but this don' t work (2 Replies)
Discussion started by: jforce
2 Replies

6. UNIX for Dummies Questions & Answers

Check the value of a string variable

hi to all, i want to check the value of a variable that it contains characters. for example i try the following: if then ......... i just want to check that in the specific line that is a variable called "passline" has the entry "password". But it can contain also other characters in the... (4 Replies)
Discussion started by: omonoiatis9
4 Replies

7. UNIX for Dummies Questions & Answers

How to check a particular element in a string variable

Hi, I have a string variable containing value say abc123 I want to check if the 3rd element of this string is "c" in a if statement.Actually i dont know the syntax of how to use substring in an if statement in shell script. Please reply soon. Regards Navjot (3 Replies)
Discussion started by: navjotsingh
3 Replies

8. Shell Programming and Scripting

read string, check string length and cut

Hello All, Plz help me with: I have a csv file with data separated by ',' and optionally enclosed by "". I want to check each of these values to see if they exceed the specified string length, and if they do I want to cut just that value to the max length allowed and keep the csv format as it... (9 Replies)
Discussion started by: ozzy80
9 Replies

9. UNIX for Dummies Questions & Answers

check whether variable number or string

I am passing an argument to a file and i wanna check whether the argument is a number or string ? how can i do this? (4 Replies)
Discussion started by: rolex.mp
4 Replies

10. Shell Programming and Scripting

How to check a string in the variable

hi, I have a variable var1 as follows in the script. var1="one two three desformat=PDF xyz" I would like to check whether $var1 has a string "desformat=PDF" or not. Is there any command I can use (not need to creat a file)? Currently, I am using this: if ( grep "desformat=PDF"... (1 Reply)
Discussion started by: josephwong
1 Replies
Login or Register to Ask a Question