Showing posts with label Unix. Show all posts
Showing posts with label Unix. Show all posts

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, October 27, 2009

Simple ASCII text files: from MS Windows to Linux

Here there is something so trivial that it'll be considered too much trivial for a real Linux geek.
However, since I'm not a Linux hyper-geek, I like to report the following. I think it'll turn out to be useful for not wasting time around for such a small detail.

State of the problem:
  • I edit a simple ASCII text file under MS Windows, for example a PBS script to run a parallel job on a compute cluster.
  • I move the file to the cluster, which, of course, runs Linux
  • I submit the script, via the qsub command. Bash is the shell interpreter in this case. It gives me the following error: ^m command not found
What the hell is going on ?
You may say: "of course, buddy, you're trying to mess up editing files in one environment and running them in another". Well, when your institution makes you use MS Windows for simple desktop computing, there is nothing you can.

Actually, you can do the following:

when the file is on the Linux machine (the front-end node of the cluster, for example), before running it pass it through the dos2unix command. It's a very nice program for converting a text file formatted under DOS/MAC into a text file format for the Unix environment.