Problem with the shell script for understanding


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Problem with the shell script for understanding
# 1  
Old 02-21-2011
Problem with the shell script for understanding

Can Anybody please tell me the meaning of the script:

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

# 2  
Old 02-21-2011
Hi,

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

Regards,
Birei
This User Gave Thanks to birei For This Post:
# 3  
Old 02-21-2011
Can you explain using individual components
thanks Smilie
# 4  
Old 02-21-2011
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

Last edited by birei; 02-22-2011 at 09:50 AM..
This User Gave Thanks to birei For This Post:
# 5  
Old 02-21-2011
Now I have understood your meaning Smilie
# 6  
Old 03-18-2011
Quote:
Originally Posted by birei
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
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.
# 7  
Old 03-18-2011
This is because - i guess - you did a typo error :

instead of
Code:
echo $str | sed 's/.*\\//'

i guess in your script you have
Code:
echo $str | sed 's/.*\///'

by the way instead of
Code:
echo $str | sed 's/.*\///'

you'd better have something like
Code:
echo ${str##*/}

or even directly
Code:
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

Code:
# 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
#

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Problem with (Understanding) function

I have this code #!/bin/bash LZ () { RETVAL="\n$(date +%Y-%m-%d_%H-%M-%S) --- " return RETVAL } echo -e $LZ"Test" sleep 3 echo -e $LZ"Test" which I want to use to make logentrys on my NAS. I expect of this code that there would be output like 2017-03-07_11-00-00 --- Test (4 Replies)
Discussion started by: matrois
4 Replies

2. Shell Programming and Scripting

Problem in understanding debugging

Hi i was going through the script debugging technique. below example was given in the book. 1 #!/bin/sh 2 3 Failed() { 4 if ; then 5 echo "Failed. Exiting." ; exit 1 ; 6 fi 7 echo "Done." 8 } 9 10 echo "Deleting old backups,... (11 Replies)
Discussion started by: scriptor
11 Replies

3. Shell Programming and Scripting

Problem on understanding the regexp command

Hi all, I'm not clear of this regexp command: regexp {(\S+)\/+$} $String match GetString From my observation and testing, if $String is abc/def/gh $GetString will be abc/def I don't understand how the /gh in $String got eliminated. Please help. Thanks (2 Replies)
Discussion started by: mar85
2 Replies

4. Shell Programming and Scripting

Problem in understanding export uses

i am beginner in shell scripting. not able to understand what below line will do. PS1=${HOST:=Žuname -nŽ}"$ " ; export PS1 HOST below is the script #!/bin/hash PS1=${HOST:=Žuname -nŽ}"$ " ; export PS1 HOST ; echo $PS1 and i getting the below output Žuname -nŽ$ (25 Replies)
Discussion started by: scriptor
25 Replies

5. UNIX for Dummies Questions & Answers

Problem understanding Paths

If I don't explain my issue well enough, I apologize ahead of time, extreme newbie here to scripting. I'm currently learning scripting from books and have moved on to the text Wicked Cool Shell Scripts by Dave Taylor, but there are still basic concepts that I'm having trouble understanding. ... (10 Replies)
Discussion started by: Chasman78
10 Replies

6. Shell Programming and Scripting

shell script behavior is strange or I'm not understanding

Hi I have wrote the small script, where $SRC=$HOME the input file is simple text file having directories in my $SRC on pre line desktop download myfiles games #!/bin/bash FILENAME=$1 ERROR_LOG="$SRC/err.$$.log" while read line do echo "########## strat Gmake $line... (6 Replies)
Discussion started by: the.reverser
6 Replies

7. Shell Programming and Scripting

Understanding a Shell Script

Hi Guys, I am absolutely a newbie to Solaris 8.0. Following is a piece of code in a script (/bin/sh). Can anybody help me in deciphering this ? Please see my questions after the script code - _____________________________________________________ /bin/rm -f mycron crontab -l | grep -v... (5 Replies)
Discussion started by: angshuman_ag
5 Replies

8. Shell Programming and Scripting

Understanding a Simple Shell Script

#! /usr/bin/ksh old=$1 new=$2 for file in *.$old ; do mv $file ${file%$old}$new done exit 0 This script i got from the forum. script changes the extension of the files say example a.txt to a.doc b.txt to b.doc c.txt to c.doc d.txt to d.doc this scipt works fine but i am not... (2 Replies)
Discussion started by: vijays3
2 Replies

9. Shell Programming and Scripting

egrep understanding problem

Hi, Can anyone please let me know the meaning of this line,i am not able to understand the egrep part(egrep '^{1,2}).This will search for this combination in beginning but what does the values in {}signifies here. /bin/echo $WhenToRun | egrep '^{1,2}:$' >/dev/null (1 Reply)
Discussion started by: namishtiwari
1 Replies

10. Filesystems, Disks and Memory

having problem in understanding namei module

can anyone give me some idea on unix filesystem namei's algorithsm (2 Replies)
Discussion started by: kangc
2 Replies
Login or Register to Ask a Question