In need of explanation


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers In need of explanation
# 1  
Old 11-20-2009
In need of explanation

Its great someone provided this script that strips out a filename and extension but can someone explain how each line works?

file1='Jane Mid Doe.txt'
newfile='Jane.txt'


1) ext=${file1##*.}
2) filename=${file%%.???}
3) set -- $filename
4) newfile="1.$extension"
# 2  
Old 11-20-2009
Hi,

Code:
Meaning of different operator.. This might help you to understand about %% and ##

Operator "#" means "delete from the left, to the first case of what follows."
         $ x="This is my test string."
$ echo ${x#* } is my test string.
Operator "##" means "delete from the left, to the last case of what follows."
$ x="This is my test string." $ echo ${x##* } string.
Operator "%" means "delete from the right, to the first case of what follows."
$ x="This is my test string." $ echo ${x% *} This is my test
Operator "%%" means "delete from the right, to the last case of what follows."
$ x="This is my test string." $ echo ${x%% *} This

Now come to your example.

Code:
file1='Jane Mid Doe.txt'
newfile='Jane.txt'
 
 
1) ext=${file1##*.}  
    ## delete from left to last match. so here pattern is '*.' so left to right last match will give txt. ( it deletes Jane Mid Doe. ). Now ext = txt

2) filename=${file%%.???} ( I think here its not file, its file1).
  %% right to left to last match. and ? matches single character
so filename will Jane Mid Doe

3) set -- $filename
This is to set the $filename as an environment. so you can access through  $1, $2.. variables.
now $1 - Jane , $2 - Mid  and $3 - Doe
4) newfile="1.$extension" 
so in this statement you have to change 1 to $1

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Function explanation

dear sir, I am new to unix zone. need some explanation on the function used. cat /apps/prd/venue/code/bin/std.funcs #!/usr/bin/ksh #------------------------------------------------------------------- # printmsg: prints the message given in arg 1 with timestamp to # stdout... (3 Replies)
Discussion started by: gowthamsoft
3 Replies

2. Shell Programming and Scripting

Need explanation

Hi, I need more explination on it, how it works abcd="$(echo "$abcd" | sed 's/ //g')" >> ${LOGFILE} 2>&1 can any one suggest me on this? Rgds, LKR (1 Reply)
Discussion started by: lakshmanraok
1 Replies

3. UNIX for Dummies Questions & Answers

Explanation on egrep

Hi all I am new to egrep can someone please explain me what does the below Statement do egrep -v "^missing sales|^\ Thanks in advance Sri Please use next time code tags for your code and data (4 Replies)
Discussion started by: Sri3001
4 Replies

4. Shell Programming and Scripting

awk explanation

Hello, I have recently come across this awk program. Can some one shed some light on what is taking place. awk '{!a++}END{for(i in a) if ( a >10 ) print a,i }' $FILE Best Regards, jaysunn (1 Reply)
Discussion started by: jaysunn
1 Replies

5. Shell Programming and Scripting

code explanation

Can you please explain the following code plz? my_cd=' ' while getopts :e: OPTION; do case "$OPTION" in e) my_cd ="$OPTARG";; esac done if ; then echo " >>> ERROR - I am wrong" echo " >>> ERROR - Hello" exit 99 fi What I don't understand is what is OPTION or... (3 Replies)
Discussion started by: RubinPat
3 Replies

6. Shell Programming and Scripting

same action in then as in else: explanation?

in /etc/init.d/networking of an ubuntu computer, I found this code: if ifdown -a --exclude=lo; then log_action_end_msg $? else log_action_end_msg $? fi Shouldn't it be replace by ifdown -a --exclude=lo ... (0 Replies)
Discussion started by: raphinou
0 Replies

7. Shell Programming and Scripting

Variable explanation

What does this part in the following code ? if ; then $t shows some time values for getting the response, but why .$t and what does the . after the != t=$(time -p wget --quiet --post-data='username=xxx&password=xxxx&id=xxxxxx' --no-check-certificate --output-document=/tmp/sms-status_out... (3 Replies)
Discussion started by: locutus01
3 Replies

8. UNIX and Linux Applications

need explanation

Hi am having a c pgm. It has the include files (unistd.h,sys/types.h,win.h,scr.h,curses.h,stdarg.h and color.h). I don't know the purpose of these include files. will u plz explain me. (1 Reply)
Discussion started by: Mari.kb
1 Replies

9. Shell Programming and Scripting

tr explanation please

how does below tr command replace nonletters with newlines? I think I understand tr -cs '\n' part.. but what is A-Za-z\' <--- what is this?? tr -cs A-Za-z\' '\n' | -c --complement -s, --squeeze-repeats replace each input sequence of a repeated character that is... (1 Reply)
Discussion started by: convenientstore
1 Replies

10. Shell Programming and Scripting

tr explanation please

how does below tr command replace nonletters with newlines? I think I understand tr -cs '\n' part.. but what is A-Za-z\' <--- what is this?? tr -cs A-Za-z\' '\n' | -c --complement -s, --squeeze-repeats replace each input sequence of a repeated character that is... (0 Replies)
Discussion started by: convenientstore
0 Replies
Login or Register to Ask a Question

Featured Tech Videos