Tag Archives: bash

Get the ip address in a shell script

Using ifconfig was not a good idea at all.
Here is a more distro-agnostic way:

ip addr show eth0 | grep "inet "| awk '{ print $2}' | awk -F '/' '{ print $1 }'


# set the language to English, to avoid translation problems with ifconfig
export LANG=C
# get IP address of eth0 network interface
ifconfig eth0 | awk '/inet addr/ {split ($2,A,":"); print A[2]}'


source

Redirecting output of the bash keyword time

time command > timelog doesn’t work, because time outputs to stderr
time command 2> timelog doesn’t work either, because time actually is a bash keyword, and it’s always run in a subshell

Redirecting the output of time can be achieved by executing the whole command in a block, then redirecting its output:
{ time command; } 2> timelog

Note: Linux also provides a time binary (/usr/bin/time), but with a different output (and might be less efficient ?)

source