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.
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:
show that they produce identical results when this script is invoked with the name of my home directory:
but, very different results if I invoke the same script with / as an operand:
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.
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).
I put the code you provided in version.sh and calling that script from another script "callversion.sh"
callversion.sh
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.
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.
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.
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)
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)
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)
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)
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)
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)
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)
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)
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)