Incrementing the New File Version Number


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Incrementing the New File Version Number
# 15  
Old 06-13-2013
I think it echoes "callversion.sh" because you source (using the .) version.sh. Make it executable and run it, then the echo will be "version.sh"
# 16  
Old 06-13-2013
Quote:
Originally Posted by Ariean
Thanks Don Awesome!!, i tested the script it's working fine with no single error. How ever i have few questions.

1) Is "${i#*[*]}" same as "${i#**}" when i tested it is giving same results, may i please know the purpose of those square brackets then, additional check or something?

2) [ "$VER_NUM" -gt "$MAX_V" ] && MAX_V=$VER_NUM
Here it will check if VER_NUM is greater than previous version and if it is then save it to MAX_V, probably another syntax or way of assigning a value to variable.

3) IAm=${0##*/} --- Trying to debug this but don't understand completely. As per my assumption you were using this in conjunction with printf to print the output? may i please know what does each of those${0##*/} represent?

4) printf "$Usage" "$IAm" "$1" >&2 --- i know you are redirecting to standard output, and you are passing Usage & IAm variables along with input argument(provider_code). Much appreciate if you could explain a bit.

Thank you.
Hi Ariean,
I believe that DGPickett has already answered your questions. I'll go into a little more detail here since it is clear that you are a programming novice.

1) In the command line:
Code:
for i in "$TGT_DIR/$FILENAME"*.xml

the "$TGT_DIR/$FILENAME"*.xml will expand to a list of all of the files in the directory named by the expansion of "$TGT_DIR and $FILENAME. For purposes of this discussion, assume that $TGT_DIR expands to /dir and $FILENAME expands to ACC. If there are no matching files in that directory, the expansion of that filename matching pattern (and the value stored in the shell variable i the 1st (and only) time through the for loop) will be the string /dir/ACC*.xml. Then the test command:
Code:
[ "$i" != "${i#*[*]}" ]

will return TRUE (exit status 0) if the expansion of i is not the same as the expansion of i with the shortest string of any number of any characters and an asterisk character removed from the start of the expansion of i. So, in this case /dir/ACC*.xml will not be the same as .xml because there was an asterisk (*) in $i. Again, in this case, "${i#**}" (which expands to an empty string) is VERY different from "${i#*[*]}" (which expands to .xml). As DGPickett said, in a filename pattern matching expression, * is a metacharacter when it is not between square brackets. In this case it matches zero or more of any character. Inside square brackets[*], it only matches the asterisk itself.

In the more common case where at least one file matches the pattern, the value stored in i will not contain an asterisk.

2) The constructs:
Code:
        [ "$VER_NUM" -gt "$MAX_V" ] && MAX_V=$VER_NUM

and
Code:
        if [ "$VER_NUM" -gt "$MAX_V" ]
        then    MAX_V=$VER_NUM
        fi

are equivalent. You should be aware of both forms although I would never suggest that it would be wrong to always use the if, then, fi version. When there are imbedded comments in the then portion of an if, as there were in an earlier if statement in that for loop, you have to be very careful not to accidentally comment out the code you want to run as well when you use the first form above.

Use whichever form is easiest for you to understand.

3) In standards conforming shells, $0 expands to the pathname the shell used to invoke this script. If your script is named version.sh, then $0 could expand to something like version.sh, ./version.sh, or /home/ariean/bin/version.sh. The assignment IAM={0##*/} (similar to the "${i#*[*]}" above) removes the longest (because it uses ## instead of #) string on any number of characters ending with a "/" from the start of the contents of $0. So, for all of the possible values of $0 shown above, ${0##*/} expands to version.sh.

With both ${var#pattern} and ${var##pattern}, var will be expanded unchanged if the given pattern does not fully match a string at the start of the contents of var.

4) I don't have anything to add to what DGPickett already said here.

I see that you have posted another question while I've been working on this response. It looks like DGPickett and RudiC have already made great suggestions about how to get the correct name in your diagnostic messages.

Last edited by Don Cragun; 06-13-2013 at 05:07 PM..
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

UNIX replacing and incrementing number

Hi I am unix newbie looking for a unix bash script that can make it easier to do my code work. we have a code number for each code block that we want to incrementally assign. We have 10000 of these and it is very laborious to do this one by one. so what we want is start from the top of the... (4 Replies)
Discussion started by: chamajid
4 Replies

2. UNIX for Beginners Questions & Answers

Incrementing the New File Version Number

Hi, This is my first post here. I am using cygwin on Windows 7. I am starting with a data file with filename "name_1.ext", like "20180831_snapgenotypes_1.csv". The "_1" before ".ext" is a version number. Integers (0-99) are sufficient. They don't have to be like "1.0.0". The filename may... (2 Replies)
Discussion started by: minimalist
2 Replies

3. UNIX for Advanced & Expert Users

Add an incrementing identity column to a file

Two questions: 1) Is there a way to create a new column (i.e. ID) to an existing file containing data and the new column ID populate as an auto incrementing number? for example: (Current file has headers and is PIPE delimited.) **data looks like this** "COL1"|"COL2"|"COL3"|"COL4"|"COL5"... (4 Replies)
Discussion started by: timlisa20
4 Replies

4. UNIX for Advanced & Expert Users

Add an incrementing identity column to a file

Two questions: 1) Is there a way to create a new column (i.e. ID) to an existing file containing data and the new column ID populate as an auto incrementing number? for example: (Current file has headers and is PIPE delimited.) **data looks like this** "COL1"|"COL2"|"COL3"|"COL4"|"COL5"... (0 Replies)
Discussion started by: timlisa20
0 Replies

5. Shell Programming and Scripting

Incrementing parts of ten digits number by parts

I have number in file which contains date and serial number: 2013101000. The last two digits are serial number (00). So maximum of serial number is 100. After reaching 100 it becomes 00 with incrementing 10 which is day with max 31. after reaching 31 it becomes 00 and increments 10... (31 Replies)
Discussion started by: Natalie
31 Replies

6. Shell Programming and Scripting

Replacing string by incrementing number

Dear all Say I have a file as ! TICKET NBR : 234 !GSI : 102 ! 3100.2.112.1 11/06/2013 15:56:29 ! 3100.2.22.3 98 ! 3100.2.134.2 8 ! ! TICKET NBR : 1809 ! GSI : 102 ! 3100.2.112.1 11/06/2013 16:00:45 ! 3100.2.22.3 65 ! 3100.2.134.2 3 ! ! TICKET NBR : 587 ! GSI : 102 ! 3100.2.112.1... (3 Replies)
Discussion started by: OTNA
3 Replies

7. Shell Programming and Scripting

Change numbers in a file, incrementing them

Hello, Here's a file of mine: key1:431 key2:159 key3:998 I need to change these keys to something bigger - and I actually need to shift them all by a range of 3. The output would be: key1:434 key2:162 key3:1001 I can't find the propper sed/awk line that would alter all my... (4 Replies)
Discussion started by: fzd
4 Replies

8. Shell Programming and Scripting

Incrementing number in bash

I have the following code and getting the error ./raytrac.bash: line 231: ((: 0++: syntax error: operand expected (error token is "+") iarg = 0 iarg=0 narg=$# # Number of arguments passed. echo "narg = $narg" argsArr=("$@") # Set... (1 Reply)
Discussion started by: kristinu
1 Replies

9. Shell Programming and Scripting

perl + array and incrementing number

morning guys and gals, I am haveing a problem, a friend helped me out with this script but i dont know how to add incrementing number for each movie in movie.list. this is what i have so far. any assistance would be great. I have removed the GT and LT symbols so you can see what is going on... (5 Replies)
Discussion started by: Optimus_P
5 Replies

10. Shell Programming and Scripting

Port incrementing using one file

Hi I wrote this script with the help of you guyz.My next challenge is to increment the port using single file.So far iam using this code to increment hp1 and hp2 .To increment port numbers, iam using two different files (.default_port_hp1 and .default_port_hp2).The challenge for me is to use... (0 Replies)
Discussion started by: coolkid
0 Replies
Login or Register to Ask a Question