Archive for the ‘bashism’ Category

Lmod and Sudo: ‘module: command not found’

September 11th, 2015 No comments

Recently, I switched from Environment Modules to Lmod because of … reasons. I really like it so far but one thing annoyed me a bit. When executing sudo -s which is one of my usual workflows, my shell reports:

module: command not found

The reason is simple: sudo dumps most of the users environment with some exceptions, due to env_reset in /etc/sudoers, when spawning the new non-login shell. Specifically the last fact causes the trouble since Lmod is activated by sourcing the proper file on (login-)shell startup. In my case (following the Lmod docs) there is a soft link in /etc/profile.d pointing to /usr/share/lmod/lmod/init/profile.

My first attempts to modify the env_keep list in /etc/sudoers didn’t work, because the module command itself is set as a shell function which AFAIK cannot be handled by that list of kept variables.

from the shell environment:

BASH_FUNC_module%%=() { eval $($LMOD_CMD bash “$@”);
[ $? = 0 ] && eval $(${LMOD_SETTARG_CMD:-:} -s sh)

$ type module
module is a function
module ()
eval $($LMOD_CMD bash “$@”);
[ $? = 0 ] && eval $(${LMOD_SETTARG_CMD:-:} -s sh)

So I saw no other solution but adding the following code snippet to my /etc/bash.bashrc

if [ -z "$LMOD_CMD" ]; then
. /etc/profile.d/

This will test if there is a LMOD_CMD environment variable set. If not, it will source the proper file to activate Lmod.

Feel free to post if there are better solution! Cheerio!

Shell One-liner: psgrep

July 6th, 2015 2 comments

When I want to find out specific process information, I usually use ps aux | grep PATTERN. There are several drawbacks, but the most annoying one for me are the missing coloumn headlines. Therefore, I made this little alias and put it into my global bash.bashrc file:

alias psgrep=’ps u | head -n 1; ps aux | grep -v grep | grep’

This enables a psgrep command which can be used like grep (since that is exactly what it is).



Maybe some of you know an even better way? Cheerio!

Categories: bashism, linux Tags: ,

Number Replacement in Filenames

October 15th, 2013 1 comment

Just assume you have a lot of files with numbers in their names, but these numbers do not have leading zeros. How would you fix that?

rename 's/\d+/sprintf("%04d",$&)/e' *.FILEENDING

This command leaves the characters in the file name and replaces all numbers with the same number and leading zeros. Works with bash and ZSH, no warranty 😉 You also can replace the “04d” with “0Xd” and you get X digit numbers.

Case Sensitivity on MacOSX

May 28th, 2013 No comments

The files system HFS+ on a MAC is kind of crooked when it comes to case sensitivity (Wikipedia: HFS+). In case you come across a piece of software that relies on case sensitivity beeing there you can use a (sparse) volume formatted with case sensitivity to get the job done. Lets use a generic name 😉


First, create the volume, mount it and save the UUID so you recognize the volume later:

hdiutil create -volname ${NAME} -type SPARSE -fs 'Case-sensitive Journaled HFS+' -size 40g ~/${NAME}.dmg
hdiutil attach ~/${NAME}.dmg.sparseimage -mountpoint /Volumes/${NAME}
diskutil info /Volumes/${NAME} | grep UUID | awk '{ print $3 }' > ~/.${NAME}voluuid

To automatically mount the volume in case it is not mounted whenever you open a shell, add the following to your ~/.bash_profile:

TMPUUID=`cat ~/.${NAME}voluuid`
MNTPNT=`diskutil info ${TMPUUID} | grep "Mount Point" | awk '{ print $3 }'`
if [ "$MNTPNT" = "" ]; then hdiutil attach ~/${NAME}.dmg.sparseimage -mountpoint /Volumes/${NAME}; fi


Categories: bashism, software Tags:

Console Emulation for Gnome

February 24th, 2012 4 comments

Once upon a time I used KDE. It was a nice and peaceful time of adventures and magic until the eldritch demons ‘eye-candy’ and ‘feature-delusion’ brought terror and doom. Many of us fled into the wastelands of the Gnome, looking for salvation. It was a life of privation on a better path of desktop environments. Often I found myself looking back to a nice tool called Yakuake, allowing me to show and hide a terminal on every desktop by a single key-stroke; what a powerful magic it was… But prayers didn’t remain unheard and Tilda (‘first person shooter console likeness terminal’, Tilda manpage) was forged be the almighty GTK (2.x).

Tilda on Gnome 3

Tilda on Gnome 3

…and even works very well on Gnome 3 😉

Categories: bashism, linux, software Tags: ,

leading zeros in bash scripts

July 18th, 2011 3 comments

I have a bash script that generates names for binaries, these names include parameters ranging from 2 to 64. I sort these then but due to the nature of alphanumeric sorting, 4 is sorted between 32 and 64 (4 is bigger than 3 but smaller than 6…). So what i need is leading zeros for the sorting to work the way i want it to work. The easiest solution i could come up with is padding zeros c-style using the command line version of printf:

PARAMLIST="2 4 8 16 24 32 40 48 56 64"
  printf "%02d\n" $PARAM

[update by bert]

sort –numeric-sort

solves the sorting issue as well 🙂

Categories: bashism Tags: , ,

Command line tool of the day: paste

July 15th, 2011 No comments

ever wanted to merge two text files horizontally, line by line? I do have a list of files were I want to associate one entry in each file with its name for plotting in gnuplot:

find ${LOG} -iname "*.log" -path "*${FILTER}*" -exec cat {} \; >times.txt
find ${LOG} -iname "*.log" -path "*${FILTER}*" -exec echo {} \; >names.txt

Now i need one result file with:

name1 time1
name2 time2

The tool i was searching for is called paste and does exactly that:

paste names.txt times.txt

Sometimes I really do love my job (but only sometimes ;))

Categories: bashism, nerdcore Tags: ,

Ubuntu Unity netspeed indicator

July 8th, 2011 No comments

One of my favorite applets in gnome is netspeed which of course wont work in unity (damn you, unity). But using the indicator version of sysmon, dstat (cool tool – check it out if you haven’t already) and some bash magic you can get something similar to work anyways.

The above link explains everything needed and you’ll end up with your netspeed being displayed in the panel . . .

Categories: bashism, linux Tags: , ,

Colorfull man pages

July 8th, 2011 7 comments

Ubuntu server installation provides colored man pages – nifty 😉
This is what has to be done to get it working in a X session terminal:

sudo aptitude install most
sudo update-alternatives --config pager

Choose “most” from the list and test it with a man pagge “man test”.

Have fun . . .

[update] As always, bert nows more 😉 :
I have found a major and a minor flaw in using most as the man pager:

Major: I use the -X option for less (by putting them into the LESS env variable [1]). This does not clear the terminal on exit of less. Thus, you see the last viewed page of the manual. This is particular helpful when looking-up a command line option, so I can copy’n’paste the option to the command line after quitting less. I can’t find a similar option for most.

Minor: less shows the name of the manual page in the bottom prompt, while most just shows ‘*stdin*’.

Footnote [1]: For the curious reader, this is the content of my $LESS: -MSiRXF. Feel free to look them up in the manual.

Categories: bashism, fun, linux, Uncategorized Tags: ,

Bash: parallel processes

May 9th, 2011 2 comments

Sometimes there is a need for spawning multiple sub processes in a shell script and do something after ALL of them have finished. The Bash has nice little features to do this very easily.

Here is an example:

./process_1 &     # fork first sub process
PID_1=$!          # get PID of first sub process
./process_2 &     # fork second sub process
PID_2=$!          # get PID of second sub process some other work...

wait  ${PID_1}     # stop execution until first process has finished
wait  ${PID_2}     # stop execution until second process has finished

#...all work is done...

exit 0

With the ampersand (&) one can fork sub processes running independent from the parent process.

To avoid busy waiting we want to stop execution of the parent script until the child has finished. So we don’t use an infinite loop with a check and a sleep or something like that, but a function called wait together with the childs’ Process ID.

The PID of a created process can be received by reading $! right after the fork. As far as I know this is mostly bash functionality and maybe is not POSIX! (correct me if I’m wrong)

Categories: bashism, software Tags: , ,