Problem with the shell script for understanding

Can Anybody please tell me the meaning of the script:

#!/bin/sh
str=$@
echo $str | sed 's/.*\\//'
exit 0

Hi,

It is removing all characters until last '\' (included) of your input to the script.

Regards,
Birei

1 Like

Can you explain using individual components
thanks :slight_smile:

Hi,

I don't understand what do you mean.

First line assigns parameters to a variable and the sed command substitutes all characters found until last '\' with nothing (deletes it). In case of none '\' found, doesn't make any modification to the string.

Regards,
Birei

1 Like

Now I have understood your meaning :slight_smile:

Hi you solved my query a long time back but I have a problem here the given script is doing action like this on a sample like:
/var/opt/ericsson/resolution/HPSC/xml/outputxml/Citrix_(3).jpg
to
Citrix_(3).jpg
which shouldn't be its execution.

This is because - i guess - you did a typo error :

instead of

echo $str | sed 's/.*\\//'

i guess in your script you have

echo $str | sed 's/.*\///'

by the way instead of

echo $str | sed 's/.*\///'

you'd better have something like

echo ${str##*/}

or even directly

echo ${@##*/}

---------- Post updated at 11:45 AM ---------- Previous update was at 11:35 AM ----------

... oooops i correct what i stated above : the ${@##*/} substitution didn't work ...(ksh)
you should keep with the str intermediate variable

# echo /whaterver/tree/or/path/here/sand
/whaterver/tree/or/path/here/sand
# set -- $(echo /whaterver/tree/or/path/here/sand)
# echo $@
/whaterver/tree/or/path/here/sand
# echo ${@##*/}
ksh: : bad substitution
# str=$@
# echo ${str##*/}
sand
#