Need help using % or like option in shell script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need help using % or like option in shell script
# 1  
Old 06-14-2010
Need help using % or like option in shell script

Hi All,
I need help from you guys.
In the shell script I have, I have a variable that has a value as follows
Code:
a=abc_20100614_12346

I want to do the following logic,
Code:
if a like '%20100614%' then 
true
else
false 
fi

Guys please help me out on this.
I tried the follwoing option
Code:
if [[ $a = 20100614* ]]; then
true
else false 
fi.

This didnt work for me.It's throwing an error message as follows
Code:
[[: not found

More info on [[ from unix system
Code:
efmsdapp:/export/home/hut$ type [[
[[ is a reserved shell keyword
efmsdapp:/export/home/hut$ help [[
ERROR: Key '[[' not found (he1)
efmsdapp:/export/home/hut$

Moderator's Comments:
Mod Comment Use code tags please, ty.

Last edited by zaxxon; 06-14-2010 at 12:07 PM.. Reason: code tags
# 2  
Old 06-14-2010
Code:
if [ -n $( echo $a | grep '20100614' ) ]

Code:
if [[ "$a" =~ "20100614" ]]


Last edited by dr.house; 06-14-2010 at 10:49 AM.. Reason: 2nd version added
This User Gave Thanks to dr.house For This Post:
# 3  
Old 06-14-2010
A couple of inexpensive, posix-compliant approaches:
Code:
#!/bin/sh

a=abc_20100614_12346
date=20100614



# One way

case $a in
    *$date*) echo MATCH;;
    *) echo NOT A MATCH;;
esac



# Another way

if [ "$a" != "${a#*$date}" ]; then
    echo MATCH
else
    echo NOT A MATCH
fi

Regards,
Alister
This User Gave Thanks to alister For This Post:
# 4  
Old 06-14-2010
Quote:
Originally Posted by dr.house
Code:
if [ -n $( echo $a | grep '20100614' ) ]

Code:
if [[ "$a" =~ "20100614" ]]

Hi,Thanks for the speed reply.
When i tried " if [ -n $( echo $a | grep '20100614' ) ] " this one
its saying syntax error.
here is the code I used,
Code:
a=21345zef
b=21345
case $a in
    *$date*) d=MATCH;;
    *)  d=NOT A MATCH;;
esac
echo $d
if [ -n $( echo $a | grep '20100614' ) ] then 
echo 'eureka'
else
echo 'try again'
fi



---------- Post updated at 10:50 AM ---------- Previous update was at 10:47 AM ----------

Quote:
Originally Posted by alister
A couple of inexpensive, posix-compliant approaches:
Code:
#!/bin/sh
 
a=abc_20100614_12346
date=20100614
 
 
 
# One way
 
case $a in
    *$date*) echo MATCH;;
    *) echo NOT A MATCH;;
esac
 
 
 
# Another way
 
if [ "$a" != "${a#*$date}" ]; then
    echo MATCH
else
    echo NOT A MATCH
fi

Regards,
Alister
Hi Alister,
Thanks for the speed reply.
The first code worked like a charm for me.
But I tried the second one also but its saying bad substituion.
Here is the code I used
Code:
a=21345zef
b=21345
if [ "$a" != "${a#*$date}" ]; then
    echo MATCH
else
    echo NOT A MATCH
fi

Moderator's Comments:
Mod Comment Use code tags please, ty.

Last edited by zaxxon; 06-14-2010 at 12:09 PM.. Reason: code tags
# 5  
Old 06-14-2010
Your shell may not support the "${var#pattern}" form of parameter substitution.

Regards,
Alister
This User Gave Thanks to alister For This Post:
# 6  
Old 06-14-2010
Quote:
Originally Posted by srini02
Code:
if [ -n $( echo $a | grep '20100614' ) ] then


[/COLOR]
Code:
if [ -n $( echo $a | grep '20100614' ) ]
then


Code:
if [ -n $( echo $a | grep '20100614' ) ] ; then





This User Gave Thanks to dr.house For This Post:
# 7  
Old 06-14-2010
Quote:
Originally Posted by dr.house
Code:
if [ -n $( echo $a | grep '20100614' ) ]; then

A couple of observations regarding this code:

1. The -n operator takes only one operand. If $a contains any IFS whitespace, the unquoted command substitution will result in more than one word and a syntax error. Since we don't know for certain anything about the range of possible values for $a, this unquoted version might be buggy. It also wouldn't hurt to double quote "$a" within the command substitution.

2. You could simplify your approach to use grep's exit status directly:
Code:
if echo "$a" | grep -q '20100614'; then

Regards,
Alister
This User Gave Thanks to alister For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Menu Driven Bash Shell Script with Default Option

Hi All, I have written a menu driven bash shell script. Current Output is as below: ------------------------------------- Main Menu ------------------------------------- Option 1 Option 2 Option 3 Option 4 Exit ===================================== Enter your... (3 Replies)
Discussion started by: kiran_j
3 Replies

2. Shell Programming and Scripting

Script to call a menu script and redirect each option to a text file

Hello, I want to design a script that will call an existing menu script and select options one by one and redirict the out put to a file. For example;- In the script MENU.sh there are 10 options i want to design a script MENU2.sh that will select option 2 3 4 6 7 10 and redirict the output... (4 Replies)
Discussion started by: spradha
4 Replies

3. Shell Programming and Scripting

Call one script option to other in shell script

HI Guys, My Script abc.sh 1) Checks 2) CA Scipt 3) CIA Script 0) Exit Enter Choice : Now if i select choice 2 then after finshed choice 2 wait for 40 min and run choice 3 what i can write in CA Scipt option: if then My Code : ... (3 Replies)
Discussion started by: pareshkp
3 Replies

4. UNIX for Dummies Questions & Answers

Executing a tar command with the --exclude option in a Debian shell script.

Hi All, I am trying to execute the following tar command with two --exclude options to suppress extract of the two directories specified. Do I need to single quote the directory paths ?? Many thanks for your help. The relevant code excerpt from the script is: cd /var/www/${SITE} ... (7 Replies)
Discussion started by: daveu7
7 Replies

5. UNIX for Dummies Questions & Answers

Invalid option errors running shell script

The script below fails with the following error messages: gzip: invalid option -- 'w' Try `gzip --help' for more information. mysqldump: Got errno 32 on write cp: invalid option -- 'w' Try `cp --help' for more information. rm: invalid option -- 'w' Try `rm --help' for more information. ... (1 Reply)
Discussion started by: ANNACTION
1 Replies

6. Shell Programming and Scripting

Executing a batch of files within a shell script with option to refire the individual files in batch

Hello everyone. I am new to shell scripting and i am required to create a shell script, the purpose of which i will explain below. I am on a solaris server btw. Before delving into the requirements, i will give youse an overview of what is currently in place and its purpose. ... (2 Replies)
Discussion started by: goddevil
2 Replies

7. Shell Programming and Scripting

recently introduced to the newer option for find...does an older option exist?

To find all the files in your home directory that have been edited in some way since the last tar file, use this command: find . -newer backup.tar.gz Is anyone familiar with an older solution? looking to identify files older then 15mins across several directories. thanks, manny (2 Replies)
Discussion started by: mr_manny
2 Replies

8. Shell Programming and Scripting

sed insert option(-i) in shell script

Hi, Please tell me how to use insert option of sed in a shell script on Solaris/AIX plateform. For example I need to insert few lines on top of every file whose name starts with say "SOL_DEL". Thanx. (2 Replies)
Discussion started by: sanjay1979
2 Replies

9. Shell Programming and Scripting

Unix Shell Script: With Menu Option

I am attempting to create a shell script with the following capaciblities: 1. Listed options to choice from 2. Use to perform awk statements 3. Print a report with the awk results My questions are 1. How do I select more than one file for option #5 and #6 2. How to I create an... (11 Replies)
Discussion started by: jroberson
11 Replies

10. Shell Programming and Scripting

Shell script "-a" option

Hi, I would like to know what exactly "-a" option does in the if conditional statement. I am sure, it is not doing the logical AND (&&) operation since it appears before the file. #!/bin/ksh for file in $* ; do if ] ; then basefile=`basename $file` cp $file... (2 Replies)
Discussion started by: royalibrahim
2 Replies
Login or Register to Ask a Question