Check if parameter passes in contains certain string


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Check if parameter passes in contains certain string
# 1  
Old 02-14-2005
Question Check if parameter passes in contains certain string

Hi Guys,
I am writing a Unix script which accepts a directory path as parameter $1

so something like

/user5.data/WA/01

will be passed in.

I want to determine if the directory path passed in contains "WA" as above (because then I need to do something specific if it does)

What is the best way to check this?

I am still fairly new to Unix scripting


Cheers

B Cunney
Smilie
# 2  
Old 02-14-2005
Computer

awk '/WA/' '{print $0}' input.lst > output.lst
for list in `cat output.lst`
do
<various code>
done

or somehting like:

awk -F/ '{print $2}' input.lst | read X
if [ "$X" == "WA" ] ; then
<various code>
fi

Last edited by gozer13; 02-14-2005 at 12:59 PM..
# 3  
Old 02-14-2005
The shell also supports different kinds of string operators. The most nearly universal one is expr - in this case expr index

Try man expr.
Code:
let is_there=0
check ()
{
  if [ $is_there -gt 0 ] ; then
     echo "$str has a WA in it"
  else
     echo "$str has no WA in it"
  fi     	
}

str="/someplace/WA/01/"
is_there=`expr index $str "WA"`
check;

Test whether expr return zero or a non-zero result.
# 4  
Old 02-14-2005
Code:
#!/bin/ksh

a='/user5/data/WA/01'

if [[ "${a}" = @(*WA*) ]] ; then
   echo "matched"
else
   echo "NOT matched"
fi;

# 5  
Old 02-14-2005
I'll just throw one more into the pot incase you're using an old UNIX without the ksh builtins, etc, and have to make do with an old version of sh....

Code:
#!/bin/sh

echo "$1" | grep "WA" >/dev/null 2>&1

if [ "$?" -eq "0" ]; then
  echo "OK"
else
  echo "No no no"
fi

exit 0

Cheers
ZB
# 6  
Old 02-14-2005
In the olden days before the arrival of modern shells, we would use the case statement to test strings. The case statement is the only built-in function in the original Bourne shell that supports regular expressions.

Code:
case $1 in
*WA*)  echo yes
       ;;
*)     echo no
       ;;
esac

# 7  
Old 02-15-2005
if [ `echo $1 |grep "WA"` ]; then
echo "Found"
else
echo "Not Found"
fi
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Duplicate check by passing external parameter

I have a code which is using to find duplicates in a files based on column.Below is the same code which is used to find duplicates in my file based on column 1 awk -F'|' '{if (x) { x_count++; print $0; if (x_count == 1) { print x } } x = $0}' FileName >Dup_File.txt But my requirement here is... (3 Replies)
Discussion started by: ginrkf
3 Replies

2. Shell Programming and Scripting

Check parameter is number or string

Hey I'm new in linux, I'm looking for a code to check whether the parameter is a number or a string. I have already tried this code: eerste=$(echo $1 | grep "^*$">aux) if But it doesn't work.:confused: Thanks (2 Replies)
Discussion started by: Eclecticaa
2 Replies

3. Shell Programming and Scripting

Check input parameter

Hi all i need to check that if user has passed any input parameter while executing he shell script like ./test1.sh -a"-v" then do smothing if user execute the script without giving input paramater then ./test1.sh then do something how can we check this input parameter (6 Replies)
Discussion started by: aishsimplesweet
6 Replies

4. UNIX for Advanced & Expert Users

How to check what are the current kernel parameter settings

Hi all, I have four (4) different UNIX flavours and I want to know whether the following commands are correct with respect to wanting to check on what are my current kernel parameter settings. I just want to clear the doubts hanging over my head whether the commands below are the right ones... (2 Replies)
Discussion started by: newbie_01
2 Replies

5. Shell Programming and Scripting

How to compare a parameter to a string

Hi, I have a parameter which is a string: set parameter = "string" I would like to compare it to various strings inside an IF conditional: if ($parameter == "string") then bla bla bla endif but it doesn't work, and I have no idea why. Thanks in advance, Shira. :) (12 Replies)
Discussion started by: shira
12 Replies

6. AIX

find {start} -ls - Two Passes?

I have a &quot;find {start} -ls&quot; command listing all files and dirs in on the whole filesystem. As part of this it lists locations that contain temporary files and, sometimes when executing, it identifies a file but produces an ERROR when trying to list it. ERROR thrown: find: bad status--... (2 Replies)
Discussion started by: apiggott
2 Replies

7. Shell Programming and Scripting

Passing a string parameter to a function

I need to pass a parameter to a function in a script. My parameter is a string. When I display the parameter within my function, I only get the first word from string I pass in. How can I make the function receive the whole string (and not terminate at the first space it encounters)?. part of... (1 Reply)
Discussion started by: fastgoon
1 Replies

8. Shell Programming and Scripting

Help with making a parameter check script.

I am making a script to check the parameters, but it seems that I can't catch the parameters by loop. $ cat sendmsg.sh #!/bin/sh ## Parameter Check i=0 max=$# while do PARAM=$${i} i=`expr ${i} + 1` echo ${PARAM} done How can I get the $1, $2, $3 by loop and set their... (2 Replies)
Discussion started by: GCTEII
2 Replies

9. Shell Programming and Scripting

How to check parameter variable?

Hello All, I have a script that will email out if the email address is specified as parameter 1. I am using ksh, and then tried the following : email=$1 Following did not work, I am getting error test -z $email test ${email:=" ") -eq " " test -n $email test ${?email} What... (4 Replies)
Discussion started by: negixx
4 Replies
Login or Register to Ask a Question