ksh code explanation


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting ksh code explanation
# 1  
Old 02-01-2017
ksh code explanation

Hi. Can somebody please explain the following lines of KSH code for me? The code checks all sub directories in a specific location which are numbered (E.g. test_01, test_02 ... etc.), then finds the one with highest number and extracts that number from the dir name into the variable num. I'd just like to understand how exactly these cmds work. Thanks.

Code:
 
        base=${f##*/} base=${base%_*}
        num=${f##*_}
        (( num > max[$base] )) && max["$base"]=$num

# 2  
Old 02-01-2017
${f##*/} uses Remove matching prefix pattern to expand variable f and remove all characters up to and including the last slash (/) character from the expansion. So if f contains /one/two/three/four the expansion will be four


${base%_*} uses Remove matching suffix pattern to expand variable base and remove all characters from the first underscore (_) to the end of the variable. So if base contains june_1234 the expansion will be june

base=${f##*/} base=${base%_*} This will temperately assign base as per first description and then use this to re-assign it as per the 2nd description. so if f=/one/two/three/a_b_c_123 base will become a_b_c and f will remain unchanged.

num=${f##*_} assign variable num to everything following the last underscore from variable f

(( num > max[$base] )) && max["$base"]=$num If interger comparison of num is greater than array max[$base] then assign max[$base] to num.

Last edited by Chubler_XL; 02-01-2017 at 10:11 PM..
These 2 Users Gave Thanks to Chubler_XL For This Post:
# 3  
Old 02-01-2017
Excellent answer. Many thanks.
# 4  
Old 02-05-2017
Just a quick follow up.

This line seems only to calculate up to 10. Afterwards num remains 1 ... it doesn't read the second digit. How can I change this code to increment past 10?

Code:
num=${f##*_} assign variable num to everything following the last underscore from variable f

Thanks a lot.
# 5  
Old 02-05-2017
This seems to be working OK for me:

Code:
for f in  test_1  test_180 test_2 test_10
do
    base=${f##*/} base=${base%_*}
    num=${f##*_}
    (( num > max[base] )) && max[base]=$num
    printf "%d  - %d\n" "$num" "${max[base]}"
done

output:
Code:
1  - 1
180  - 180
2  - 180
10  - 180

Could be if you have some leading zero numbers bash is trying to convert them to octal try:

Code:
for f in  test_0001  test_0180 test_0002 test_0010
do
    base=${f##*/} base=${base%_*}
    ((num=10#${f##*_}))
    (( num > max[base] )) && max[base]=$num
    printf "%d  - %d\n" "$num" "${max[base]}"
done


Last edited by Chubler_XL; 02-05-2017 at 06:13 PM..
# 6  
Old 02-05-2017
Thanks. Actually I should add that I'm trying to create a new directory (incrementing it by 1) every time the code runs. E.g. test_1, test_2, test_3 ...
But when it comes to creating the test_10 the directory it fails as it thinks it already exists .. it just sees test_1 again.
# 7  
Old 02-05-2017
Quote:
Originally Posted by general_franco
Thanks. Actually I should add that I'm trying to create a new directory (incrementing it by 1) every time the code runs. E.g. test_1, test_2, test_3 ...
But when it comes to creating the test_10 the directory it fails as it thinks it already exists .. it just sees test_1 again.
Show us the code that you think should do this. Nothing you have shown us makes any attempt to increment anything by 1.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Homework & Coursework Questions

Minix 3 Source Code Organisation Explanation

1. I've been asked to provide a detailed description on how Minix's source code is organised. This may sound slightly simple, but what exactly am I to refer to when explaining? Any documents that would provide assistance to understand whats being asked? Thanks 2. Not applicable: 3.... (1 Reply)
Discussion started by: Menace12
1 Replies

2. UNIX for Dummies Questions & Answers

Code explanation?

I need someone to tell me how exactly works the following code αfter /etc/passwd :eek:: cat /etc/passwd|grep "^:"|sed '1,$s/^\(*\):*:\(*\):.*$/ \1 \2 /'|sort -nrk3 -t:I want a good explanation to understand the code please (4 Replies)
Discussion started by: kotsos13
4 Replies

3. UNIX for Dummies Questions & Answers

Explanation of NAWK Code

Hi all, I need your help in understanding the meaning and syntax of the below nawk line. Here is an extract of a script which I use daily and works well. The script extracts the hostnames and messages within a syslog file. I would also like to extract the message time in the 3rd column by... (2 Replies)
Discussion started by: wthomas
2 Replies

4. Shell Programming and Scripting

translate ksh code to csh code

hi all, Can any 1 help me translate this korn shell code to C shell code : email=$(grep "^$1" $folder/config_2.txt | awk '{print $2'}) In config_2.txt the content is : which mean in korn shell , $1=groupname and $2=email address. Now i need to write in C shell script,when i set the... (2 Replies)
Discussion started by: proghack
2 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. Homework & Coursework Questions

Please help with the following fork code..with complete explanation

I have the following piece of codes. Please explain it to me in great detail how are these codes working. 1. #include <stdio.h> int main(){ int x; x=0; while (x<2 && fork()){ if (!fork()) execlp("echo","x++","x",0); x++; system("echo x+x"); } } 2. #include <stdio.h> int i;... (1 Reply)
Discussion started by: prakashabii
1 Replies

7. Shell Programming and Scripting

nawk code explanation

Hi I have a line/command which greps certain pattern () from the file and 22 lines AFTER that: nawk '/\/{c=22}c&&c--' input_file Can somebody explain the coding in this command (what " c&&c-- " does)? and how to modify this command to output 22 lines BEFORE this pattern () Thanks a lot ... (3 Replies)
Discussion started by: aoussenko
3 Replies

8. Shell Programming and Scripting

AWK deifnition and some small code explanation

Could someone give me a quick simple explanation for the AWK command. And also help me to explain the code i have made. I have made some general comments about it myself. I was wondering if people could help me with the rest: awk -F'' 'END { fmt = "%-20s\t%s\t%s\n" ... (0 Replies)
Discussion started by: amatuer_lee_3
0 Replies

9. Shell Programming and Scripting

Need explanation for the syntax(code)

Hi I am new to shell script programming... want to know the process of the following: if then echo "$0: missing argument for option(s) :$MISSINGOPTARG" echo "usage" $USAGE" exit 1 fi (1 Reply)
Discussion started by: chandhar
1 Replies

10. Shell Programming and Scripting

ksh syntax explanation - from mimetool

Hi I got this part of the script from the mimetool by Perderabo. I have difficulty in decyphering the syntax specially lines 4,5 & 9. Also the test condition in line 3. Could someone help me on this please. -------------------------------------- pwentry=$(grep "^$(logname):" /etc/paswd)... (2 Replies)
Discussion started by: chaandana
2 Replies
Login or Register to Ask a Question