Sponsored Content
Top Forums UNIX for Dummies Questions & Answers Incrementing the New File Version Number Post 302820843 by Don Cragun on Thursday 13th of June 2013 02:09:31 PM
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 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
Version::Next(3pm)					User Contributed Perl Documentation					Version::Next(3pm)

NAME
Version::Next - increment module version numbers simply and correctly VERSION
version 0.002 SYNOPSIS
use Version::Next; my $new_version = next_version( $old_version ); DESCRIPTION
This module provides a simple, correct way to increment a Perl module version number. It does not attempt to guess what the original version number author intended, it simply increments in the smallest possible fashion. Decimals are incremented like an odometer. Dotted decimals are incremented piecewise and presented in a standardized way. If more complex version manipulation is necessary, you may wish to consider Perl::Version. USAGE
This module uses Sub::Exporter for optional exporting. Nothing is exported by default. "next_version" my $new_version = next_version( $old_version ); Given a string, this function make the smallest logical increment and returns it. The input string is very minimally checked that it resembles a version number. Given "undef", the function returns 0. Decimal versions are incremented like an odometer, preserving the original number of decimal places. If an underscore is present (indicating an "alpha" version), its relative position is preserved. Examples: 0.001 -> 0.002 0.999 -> 1.000 0.1229 -> 0.1230 0.12_34 -> 0.12_35 0.12_99 -> 0.13_00 Dotted-decimal versions have the least significant element incremented by one. If the result exceeds 999, the element resets to 0 and the next most significant element is incremented, and so on. Any leading zero padding is removed. Examples: v1.2.3 -> v1.2.4 v1.2.999 -> v1.3.0 v1.999.999 -> v2.0.0 v1.2.3_4 -> v1.2.3_5 v1.2.3_999 -> v1.2.4_0 SEE ALSO
o Perl::Version AUTHOR
David Golden <dagolden@cpan.org> COPYRIGHT AND LICENSE
This software is Copyright (c) 2010 by David Golden. This is free software, licensed under: The Apache License, Version 2.0, January 2004 perl v5.10.1 2010-10-01 Version::Next(3pm)
All times are GMT -4. The time now is 02:13 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy