Archive

Posts Tagged ‘bash’

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

psgrep

 

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

Categories: bashism, linux 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"
for PARAM in $PARAMLIST;
do 
  printf "%02d\n" $PARAM
done

[update by bert]

sort –numeric-sort

solves the sorting issue as well 🙂

Categories: bashism 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:

#!/bin/bash
./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

#...do 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: , ,

Pwned by error message

August 17th, 2009 6 comments

In the life of an IT-guy you often shake your head and smile while wondering about the current problem or its solution. So after a while not much can realy surprise you. But last week there was such a rare moment.

The problem is an easy one: we could not execute binaries. We have a cluster here with an 32 bit head node and 64 bit compute nodes both x86 Debian GNU Linux. The stuff to be shared comes over a NFS. Heterogenouse clusters have some difficulties because you cannot execute binaries from one architecture on another. This will fail with the error message: “wrong ELF format”.
Our problem first appeared by using the Modules-tool. For testing we executed it directly:

./modulecmd
-bash: ./modulecmd: no such file or directory

But OK, as everybody could see, it was. We checked and changed the rights for users, groups, owndership, parent directories etc. Nothing helped. Next we put an eye on NFS. No miss-configuration or other distinctive feature.
Now we use the bigger weapons and strace the command.

strace ./modulecmd
execve("./modulecmd", ["./modulecmd"], [/* 25 vars */]) = -1 ENOENT (No such file or directory)
dup(2)                                  = 3
fcntl(3, F_GETFL)                       = 0x8002 (flags O_RDWR|O_LARGEFILE)
fstat(3, {st_mode=S_IFCHR|0620, st_rdev=makedev(136, 0), ...}) = 0
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7ff4b4700000
lseek(3, 0, SEEK_CUR)                   = -1 ESPIPE (Illegal seek)
write(3, "strace: exec: No such file or dir"..., 40strace: exec: No such file or directory
) = 40
close(3)                                = 0
munmap(0x7ff4b4700000, 4096)            = 0
exit_group(1)                           = ?

Also nothing surprising in here.
The further isolate the root of the behaviour, we code a short “hello world” in C and do compiling and execution and it worked. So here we got the main point which confused us. Self created binaries and all system tools worked properly, just the shared ones or let’s say “not system contained” executables failed.
OK, maybe it’s situated in a deeper level. We installed dash and try to execute it in there. No changes. Slowly but steady we ran out of ideas.
From the strace run we could see that execve backfired. This command overwrites the current forked process with the binary given by a path as an argument and executes it. So maybe the reason belongs to the execution context. We tried

bashism

instead. So WTF? After ignoring some charset ideas in mind, we arrived where we started. An obligatory file on a system executable and our binary shared over NFS told us the wrong ELF type. The installation of ia32-libs fixed the problem finally, but why there was a “no such file of directory” instead of a “wrong ELF type” we still do not know.

You are welcome to give us solution or hints.

Categories: bashism, linux Tags: , ,

compiling from sources

June 26th, 2009 No comments

I’ve been hacking around the last 4 days trying to build up a stack of 5 hpc tools all depending on each other each with a special set of features enabled – pure horror. After switching between cutting edge versions and wildly interchanging compiler-suites all with different non-positive outcome I think I’m finally done. Man vs machine 1:0 – gotcha 😉

Here 2 of the more interesting snippets I had to create to get things up an running:

# add missing string.h
echo "#include  string.h" >swap.c
cat ./SRC/SRC/mpo_LogCube3.cc >>swap.c
mv swap.c ./SRC/SRC/mpo_LogCube3.cc
# fix wrong marmotcc (missing scalasca-lib)
sed -e 's/-lcube3/-lcube3 -lsc.z/g' `which marmotcc` >marmotcc
chmod +x marmotcc
mv ./marmotcc `which marmotcc`

I had to use dos2unix as one script had windows coding and so my shell was complaining about not knowing /bin/bash^M and so on. I also wrote a script creating ./configure lines with all possible combinations of a set of flags to test which combination actually works . . .
Sometimes I’d really like those days back were a “cc hello.c -o HelloWorld” was all that was needed.

BNW

Categories: bashism, c/c++, linux, software Tags: , , ,

It’s about time

June 17th, 2009 1 comment

The tiny tool time everybody should know, measures execution time of a given command. For example:

time echo "test"

prints time information to the console. But what if you want to collect these statistics in a file? Linux man page say “-o FILE” is the way to do so. This will look like

time  -o file.log echo "test"

This FAILS not only on my machine with the error message “bash -o: unknown command” or equal. The key is to give the full path:

/usr/bin/time -o file.log echo "test"

The problem is, that “time” is used for different things. This leads to another cute tool called type. type gives information about the kind of a command.

type -a time
 
time is a shell keyword
time is /usr/bin/time

As you can see “time” not only means the command. If somebody knows more about the “Why the hell is this so?”, feel free to post 😀

Categories: bashism, linux, software Tags: , , , ,

Disable bell on every terminal types

April 1st, 2009 No comments

Just put the following line into /etc/profile or similar and your Terminal will ‘never’ annoy you again.

setterm -blength 0

Categories: linux Tags: , ,

ZIH nameserver für NRtm

February 26th, 2009 No comments

da bei uns im NRTM die ZIH und HRSK names nicht ganz resolved werden – also gar nicht – hier ein Script, dass nach dem Verbinden zum Netzwerk gestartet werden kann:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#!/bin/sh
# already inserted?
ALREADY_IN_FILE=`cat /etc/resolv.conf | grep \#\  \ auto\ inserted | wc -l`
if [ $ALREADY_IN_FILE -ge 1 ]
then
	echo "already inserted"
	exit 0
fi
# am i root?
if [ `id -u` != "0" ]
then
	echo "This must be run as root, try"
	echo "sudo ${0}"
	exit 1
fi
# insert
echo "#  auto inserted" >> /etc/resolv.conf
echo "search xxx.de xxy.de" >> /etc/resolv.conf
echo "nameserver ip1" >> /etc/resolv.conf
echo "nameserver ip2" >> /etc/resolv.conf

Nach einem neuen Verbinden ins DHCP Netzwerk muss es leider wieder eingetragen werden. IP adressen und so gibts auf anfrage.

Categories: software Tags: , , ,

nanoblogger

February 23rd, 2009 No comments

nanoblogger powered by vim, bash, cat, grep, sed, and nb 3.4 RC2 sehr genial. Bloging Software die per bash scripten statisches html erzeugt und per command line gesteuert wird.

Categories: software Tags: , ,