Incrementing the New File Version Number


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Incrementing the New File Version Number
# 8  
Old 06-13-2013
1) The square brackets ensure * is a literal not a wild card metacharacter.

2) Using boolean as procedural control of flow amuses some, but I like to use procedural language for control of flow, for maintainability. Isn't the new version max plus 1?

3) ${0##*/} says take $0, pound really hard on the nose until it is removed down to the last slash, or in other words, basename, entry name of $0. One # means pound soft, stop at first / or whatever follows. The alternative operator is %, as in you get your percent in the end, so it beats on the end not not nose. The directory containing $0 would be ${0%/*}, take off the end to the first /. Of course, if $0 has no /, that malfunctions.

4) Not standard output >&1 or nothing, but standard error >&2 Printf takes first a template (usage) with meta-strings in it that say where to insert the additional parameters. Printf "A%sC", "B" prints ABC.
This User Gave Thanks to DGPickett For This Post:
# 9  
Old 06-13-2013
Quote:
Originally Posted by DGPickett
Fear of pipes can and should be overcome! But some tools like basename are silly, dated, when you can just ${full_path##*/} and save a fork() and exec().
I'm not sure if this comment was directed at my suggestion to eliminate the ls|head pipeline. For the record, I am not on a mission to get rid of pipelines. And, when speed isn't a critical issue in a shell script, I sometimes use a pipeline (even when avoiding it could produce faster code) if the pipeline is "easier to read and understand."

But, when globbing could yield a fork error due to ARG_MAX limits, and an ls -t | head -1 pipeline would sometimes yield the wrong file and result in the destruction of data, I much prefer a different solution that avoids both issues.

Note also that the basename and dirname utilities aren't quite as easy to replace as you're suggesting. The commands:
Code:
ddir=$(dirname "$1")
sdir=${1%/*}
bbase=$(basename "$1")
sbase=${1##*/}
printf "directory containing \"%s\" is \"%s\" or \"%s\"\n" "$1" "$ddir" "$sdir"
printf "last component of \"%s\" is \"%s\" or \"%s\"\n" "$1" "$bbase" "$sbase"

show that they produce identical results when this script is invoked with the name of my home directory:
Code:
directory containing "/Users/dwc" is "/Users" or "/Users"
last component of "/Users/dwc" is "dwc" or "dwc"

but, very different results if I invoke the same script with / as an operand:
Code:
directory containing "/" is "/" or ""
last component of "/" is "/" or ""

I can certainly modify the script above to check for the special case when the operand is root; but I will frequently use basename and dirname because I don't have to think about the special case (even though a fork and exec is involved). And, what is going on will be more obvious to naive readers of my code. If this is done once during the initialization of a long running script, I might not care about the small additional start up costs. However, if this were to be done every time in a tight loop processing thousands of files that might include "/", it would be a different issue.
# 10  
Old 06-13-2013
Yes, '/' is often a painful exception in many algorithms! People who enter directories with a trailing '/' can also mess you up. For instance, on some systems "find /tmp/" produces paths like "/tmp//xxx". Of course, you can clean then with ${xxx%/} if they are not '/' (root).
# 11  
Old 06-13-2013
I put the code you provided in version.sh and calling that script from another script "callversion.sh"

callversion.sh
Code:
#!/bin/bash
. /home/infrmtca/bin/dwh_Loadfuncs.sh
. /home/infrmtca/bin/version.sh 30
echo $NEW_FILE_NAME

And when i run the script "callversion.sh" it is giving me the following output, How can i make it print "version.sh" instead of "callversion.sh". I guess i don't have any other simple option other than replacing the {0##*/} with actual script name. please let me know if this is possible any other means. Thank you.

Code:
Usage: callversion.sh Provider_Code
        Provider_Code "30" unknown
Valid Provider_Code values are:
Provider_Code   Provider
=============   ============
         1      AAC
         4      AGFIRST
         5      AGRIBANK
         6      COBANK
         7      COLUSA-GLENN
         8      FCSA
        11      HAWAII
        12      IDAHO
        16      FCBTEXAS
        17      WAC
        18      AGVANTIS
        20      YOSEMITE
        21      FPI

# 12  
Old 06-13-2013
move the file to that name.
# 13  
Old 06-13-2013
callversion.sh is invoking the version.sh, those are 2 different scripts i want the script to print it as version.sh instead of callversion.sh, if i move it i will replace one with another.
# 14  
Old 06-13-2013
if version calls callversion, you could source it instead (. filename), and then $0 would be version. Better is to make the callversion bits functions inside version, either explicitly or by sourcing the callversion file that is rewritten to be all functions.
 
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