cut and store last value of a variable into other

Hi All, I am a newbie to unix.starting my career in unix.need 1 help from you all..pls help..

i am passing a file name "abc_delta" as argument to my script1.sh.
if file name contains "_delta" at last then echo pass else fail.how to fix it.

Note:file name will always contain "_delta" at last.
ex- c_delta,upc_u_delta,12_xyz_delta etc

--------------------------------------------------------
script1.sh code:

if test $1=='_delta' 
then
echo 'pass';
else
   echo 'fail';
fi

--------------------------------------------------------
what i need to write in if condition to solve my issue..

user@ubuntu:~/programs$ cat script1.sh 
#!/bin/sh
echo $1 | grep -q "_delta"
if [ $? -eq 0 ]
then
    echo pass
else
    echo fail
fi

user@ubuntu:~/programs$ ./script1.sh abc_delta
pass

One more alternative using awk:

#!/bin/sh 
a=`echo $1|awk '/_delta/{ print 1}'` 
if [ $a -eq 1 ] 
then     
echo pass
else     
echo fail 
fi
1 Like

thnks balajesuri n pandeesh..both is working but when im passing filename for ex- abc_delta_hk then too message "pass" returned.my case is if at last there is "_delta" then only pass.

In ur post, grep searching for "_delta" anywhere in filename which i dnt want.kindly reply.

try as follows in balajesuri code:

echo $1 | grep -q "_delta$"

instead of

 echo $1 | grep -q "_delta"
1 Like

Hi ,

try this ..

#!/bin/bash
 grep _delta$ $1
if [ $? -eq 0 ]
then
    echo pass
else
    echo fail
fi
2 Likes

hi,
try this:

#!/bin/sh  
a=`echo $1|awk '/_delta$/{ print 1}'`
if [ $a -eq 1 ]  
then    
echo pass
else
echo fail
fi
1 Like
d=`echo $1|cut -d"_" -f2`
if [ "a$d" = "adelta" ]
then
  echo pass
else
  echo fail
fi

hey pradeep ,

so, now are you able to solve the issue?

thanks to all of u...great now its fine..( u used "_delta$" for matching at d end .. i got it..)

1 more way i used as below..kindly hav a look n reply ur view..

filename=$(echo $1 | awk '$0=$NF' FS="_" )
if "$filename"=="delta"
then 
echo pass
else 
echo fail
fi

This one works fine.