Monday, May 25, 2020
Integer arithmetics with environment variables in Bash
Here there's the trivial problem type: I have a collection of files enumerated with an index with format %03i, according to the usual C-like syntax, e.g.,
fileName001.dat
fileName002.dat
fileName003.dat
....
fileName00N.dat
I need to rename these files such that the second file in that series gets index 003, the 3rd file gets index 004 and so on. I essentially need to increase of one unit the value of each file index and then print it in the right format.
Integer arithmetics is possible in Bash using the arithmetic evaluation compound command
(( <expression> ))
where <expression> is the arithmetic expression involving environment variables previously assigned integer values.
To solve the previous problem,
export I=2
J=$((I+1))
echo $J
3
printf -v COUNTERNEW "%03d" "$J"
echo $COUNTERNEW
003
printf -v COUNTEROLD "%03d" "$I"
mv fileName${COUNTEROLD}.dat fileName${COUNTERNEW}.dat
See more information about the arithmetic operation compound command at http://wiki.bash-hackers.org/syntax/ccmd/arithmetic_eval.
Tuesday, April 14, 2020
Initialization of BASH sessions under Cygwin
If you are using GNU-Linux terminals under Microsoft (MS) Windows via Cygwin (which you should because MS Windows terminals, both the simple command prompt and the PowerShell are simply much, much, much ... powerful than a GNU-Linux terminal/shell), for example, the standard BASH Cygwin terminal, keep in mind that when you open such terminal, by default a login shell session is started.
When a login shell session is started, the usual BASH initial configuration file .bashrc is not automatically run ("sourced").
In addition, by default, no such BASH configuration file (.bashrc) originally exists.
If you want to create one and to put in it commands and function calls which you need at every BASH session, you need either to manually source such file after the terminal has been opened (buhhhh! very dumb and boring!!) or you need to automatically source it. How to do that?
What is automatically sourced at startup is the file .bash_profile (if you have created such file, by default it does not exist as well).
Thus, create a .bash_profile in your home folder, create a .bashrc file in the same folder and add the following BASH conditional statement inside the .bash_profile file:
if [ -f ~/.bashrc ]; then
source ~/.bashrc
fi
See here for more information.
When a login shell session is started, the usual BASH initial configuration file .bashrc is not automatically run ("sourced").
In addition, by default, no such BASH configuration file (.bashrc) originally exists.
If you want to create one and to put in it commands and function calls which you need at every BASH session, you need either to manually source such file after the terminal has been opened (buhhhh! very dumb and boring!!) or you need to automatically source it. How to do that?
What is automatically sourced at startup is the file .bash_profile (if you have created such file, by default it does not exist as well).
Thus, create a .bash_profile in your home folder, create a .bashrc file in the same folder and add the following BASH conditional statement inside the .bash_profile file:
if [ -f ~/.bashrc ]; then
source ~/.bashrc
fi
See here for more information.
Subscribe to:
Posts (Atom)