You are not logged in.

#1 2013-01-11 21:25:48

Beelzebud
Member
From: Illinois, U.S.
Registered: 2010-07-16
Posts: 154

Need some tips for a bash script.

This isn't anything important, but I'd like to figure this out.   I use a script to output a nice header when a terminal is opened.  Here is the script:

#! /bin/bash

GREEN="\033[0;32m"
LTGRN="\033[1;32m"
LBLUE="\033[1;36m"
DBLUE="\033[0;34m"
DGRAY="\033[1;30m"
NOCLR="\033[0m"

ROOT=`df -h /dev/sdb4 | grep -vE '^Filesystem' | awk '{print $4}'`
SDA4=`df -h /dev/sda4 | grep -vE '^Filesystem' | awk '{print $4}'`
SDB3=`df -h /dev/sdb3 | grep -vE '^Filesystem' | awk '{print $4}'`
IFCONF=`ip addr | grep "inet 192.168" | awk '{print $2}' | cut -d '/' -f1`
UPTIME=`uptime | awk '{for ( i = 3; i <= NF-7; i++ ) if ( i != NF-7 ) 
print $i; else print substr($i,1,length($i)-1);}'`

rm -f /etc/issue
/usr/bin/clear >> /etc/issue

echo -e $LBLUE'              +     ' >> /etc/issue
echo -e $LBLUE'              #          _____                 __      ' >> /etc/issue
echo -e $LBLUE'             ###        /  _  \_______   ____ |  |__   ' >> /etc/issue
echo -e $LBLUE'            #####      /  /_\  \_  __ \_/ ___\|  |  \  ' >> /etc/issue
echo -e $LBLUE'            ######    /    |    \  | \/\  \___|   Y  \ ' >> /etc/issue
echo -e $LBLUE'           ; #####;   '$DBLUE'\____|__  /__|    \___  >___|  / ' >> /etc/issue
echo -e $LBLUE'          +##.#####  '$DBLUE'         \/ '$LBLUE'Arch'$DBLUE' Linux \/     \/' >> /etc/issue
echo -e $LBLUE'         +##########    ' >> /etc/issue
echo -e $LBLUE'        ######'$DBLUE'#####'$LBLUE'##;   '$GREEN'System:   '$LTGRN''$(uname -m) $(uname -o)>> /etc/issue
echo -e $LBLUE'       ###'$DBLUE'############'$LBLUE'+   '$GREEN'Kernel:   '$LTGRN''$(uname -r)    >> /etc/issue
echo -e $LBLUE'      #'$DBLUE'######   #######    '$GREEN'Local IP: '$LTGRN''$IFCONF       >> /etc/issue
echo -e $DBLUE'    .######;     ;###;`".   '$GREEN'Hostname: '$LTGRN''$(hostname)          >> /etc/issue
echo -e $DBLUE'   .#######;     ;#####.     '$GREEN'/root:    '$LTGRN''$ROOT Free   >> /etc/issue
echo -e $DBLUE'   #########.   .########`    '$GREEN'/sda4:    '$LTGRN''$SDA4 Free  >> /etc/issue
echo -e $DBLUE'  ######`           `######    '$GREEN'/sdb3:    '$LTGRN''$SDB3 Free >> /etc/issue
echo -e $DBLUE' ;####                 ####;    '$GREEN'Uptime:   '$LTGRN''$UPTIME >> /etc/issue
echo -e $DBLUE' ##`                     `##     ' >> /etc/issue
echo -e $DBLUE' #`                        `#     '$DGRAY"$(date)" >> /etc/issue
echo -e $DBLUE'`                            `'$NOCLR'' >> /etc/issue
cp -f /etc/issue /etc/issue.net

I use a cron job wich executes this script every 5 minutes, and then when a terminal is opened you see this nice header and info. 

If I execute the script manually the Local IP is correctly displayed.   When the cronjob (which is under root) executes it leaves the output from the Local IP command blank.   

Is there a reason the cronjob won't display the ip, while manually executing it will?   Back when ifconfig was used, this would output with no problems, but with 'ip addr' there seems to be an issue.

Thanks for your time.

Offline

#2 2013-01-11 22:38:01

jasonwryan
Anarchist
From: .nz
Registered: 2009-05-09
Posts: 20,099
Website

Re: Need some tips for a bash script.

Have you tried running the cronjob from your user's crontab? Does that make a difference?

Also, your awk line could be shortened:

awk '/192/ {sub(/\/24/, ""); print $2}' <(ip addr)

Arch + dwm   •   Mercurial repos  •   Github

Registered Linux User #482438

Offline

#3 2013-01-12 01:22:19

oliver
Member
Registered: 2007-12-12
Posts: 433

Re: Need some tips for a bash script.

does it fail if you fully qualify the ip command?  (i.e. /sbin/ip)

You can always execute it from cron with a -x and send the output to a file and see exactly what is happening.  To do this, change cron to

/bin/bash -x /path/to/script > /tmp/outfile 2>&1

Offline

Board footer

Powered by FluxBB