10 Tools To Add Some Spice To Your UNIX Shell Scripts

by Vivek Gite on April 19, 2010 · 83 comments

There are some misconceptions that shell scripts are only for a CLI environment. You can easily use various tools to write GUI and/or network (socket) scripts under KDE or Gnome desktops. Shell scripts can make use of some of the GUI widget (menus, warning boxs, progress bars etc). You can always control the final output, cursor position on screen, various output effects, and so on. With the following tools you can build powerful, interactive, user friendly UNIX / Linux bash shell scripts.

Creating GUI application is not just expensive task but task that takes time and patience. Luckily, both UNIX and Linux ships with plenty of tools to write beautiful GUI scripts. The following tools are tested on FreeBSD and Linux operating systems but should work under other UNIX like operating systems.

#1: notify-send Command

The notify-send command allows you to send desktop notifications to the user via a notification daemon from the command line. This is useful to inform the desktop user about an event or display some form of information without getting in the user's way. You need to install the following package:
$ sudo apt-get install libnotify-bin
In this example, send simple desktop notification from the command line, enter:

 
notify-send "rsnapshot done :)"
 

Sample outputs:

Fig:01: notify-send in action

Fig:01: notify-send in action


Here is another code with additional options:

 
....
alert=18000
live=$(lynx --dump http://money.rediff.com/ | grep 'BSE LIVE' | awk '{ print $5}' | sed 's/,//g;s/\.[0-9]*//g')
[ $notify_counter -eq 0 ] && [ $live -ge $alert ] && { notify-send -t 5000 -u low -i   "BSE Sensex touched 18k";  notify_counter=1; }
...

Sample outputs:

Fig.02: notify-send with timeouts and other options

Fig.02: notify-send with timeouts and other options


Where,

  • -t 5000: Specifies the timeout in milliseconds ( 5000 milliseconds = 5 seconds)
  • -u low : Set the urgency level (i.e. low, normal, or critical).
  • -i gtk-dialog-info : Set an icon filename or stock icon to display (you can set path as -i /path/to/your-icon.png).

For more information on use of the notify-send utility, please refer to the notify-send man page, viewable by typing man notify-send from the command line:
man notify-send

#2: tput Command

The tput command is used to set terminal features. With tput you can set:

  • Move the cursor around the screen.
  • Get information about terminal.
  • Set colors (background and foreground).
  • Set bold mode.
  • Set reverse mode and much more.

Here is a sample code:

#!/bin/bash
 
# clear the screen
tput clear
 
# Move cursor to screen location X,Y (top left is 0,0)
tput cup 3 15
 
# Set a foreground colour using ANSI escape
tput setaf 3
echo "XYX Corp LTD."
tput sgr0
 
tput cup 5 17
# Set reverse video mode
tput rev
echo "M A I N - M E N U"
tput sgr0
 
tput cup 7 15
echo "1. User Management"
 
tput cup 8 15
echo "2. Service Management"
 
tput cup 9 15
echo "3. Process Management"
 
tput cup 10 15
echo "4. Backup"
 
# Set bold mode
tput bold
tput cup 12 15
read -p "Enter your choice [1-4] " choice
 
tput clear
tput sgr0
tput rc

Sample outputs:

Fig.03: tput in action

Fig.03: tput in action


For more detail concerning the tput command, see the following man page:
man 5 terminfo
man tput

#3: setleds Command

The setleds command allows you to set the keyboard leds. In this example, set NumLock on:

setleds -D +num

To turn it off NumLock, enter:

setleds -D -num
  • -caps : Clear CapsLock.
  • +caps : Set CapsLock.
  • -scroll : Clear ScrollLock.
  • +scroll : Set ScrollLock.

See setleds command man page for more information and options:
man setleds

#4: zenity Command

The zenity commadn will display GTK+ dialogs box, and return the users input. This allows you to present information, and ask for information from the user, from all manner of shell scripts. Here is a sample GUI client for the whois directory service for given domain name:

#!/bin/bash
# Get domain name
_zenity="/usr/bin/zenity"
_out="/tmp/whois.output.$$"
domain=$(${_zenity} --title  "Enter domain" \
	            --entry --text "Enter the domain you would like to see whois info" )
 
if [ $? -eq 0 ]
then
  # Display a progress dialog while searching whois database
  whois $domain  | tee >(${_zenity} --width=200 --height=100 \
  				    --title="whois" --progress \
			            --pulsate --text="Searching domain info..." \
                                    --auto-kill --auto-close \
                                    --percentage=10) >${_out}
 
  # Display back output
  ${_zenity} --width=800 --height=600  \
	     --title "Whois info for $domain" \
	     --text-info --filename="${_out}"
else
  ${_zenity} --error \
	     --text="No input provided"
fi

Sample outputs:

Fig.04: zenity in Action

Fig.04: zenity in Action


See the zenity man page for more information and all other supports GTK+ widgets:
zenity --help
man zenity

#5: kdialog Command

kdialog is just like zenity but it is designed for KDE desktop / qt apps. You can display dialogs using kdialog. The following will display message on screen:

 
kdialog --dontagain myscript:nofilemsg --msgbox "File: '~/.backup/config' not found."
 

Sample outputs:

Fig.05: Suppressing the display of a dialog

Fig.05: Suppressing the display of a dialog

See shell scripting with KDE Dialogs tutorial for more information.

#6: Dialog

Dialog is an application used in shell scripts which displays text user interface widgets. It uses the curses or ncurses library. Here is a sample code:

>#!/bin/bash
dialog --title "Delete file" \
--backtitle "Linux Shell Script Tutorial Example" \
--yesno "Are you sure you want to permanently delete \"/tmp/foo.txt\"?" 7 60
 
# Get exit status
# 0 means user hit [yes] button.
# 1 means user hit [no] button.
# 255 means user hit [Esc] key.
response=$?
case $response in
   0) echo "File deleted.";;
   1) echo "File not deleted.";;
   255) echo "[ESC] key pressed.";;
esac

See the dialog man page for details:
man dialog

A Note About Other User Interface Widgets Tools

UNIX and Linux comes with lots of other tools to display and control apps from the command line, and shell scripts can make use of some of the KDE / Gnome / X widget set:

  • gmessage - a GTK-based xmessage clone.
  • xmessage - display a message or query in a window (X-based /bin/echo)
  • whiptail - display dialog boxes from shell scripts
  • python-dialog - Python module for making simple Text/Console-mode user interfaces

#7: logger command

The logger command writes entries in the system log file such as /var/log/messages. It provides a shell command interface to the syslog system log module:

 
logger "MySQL database backup failed."
tail -f /var/log/messages
logger -t mysqld -p daemon.error "Database Server failed"
tail -f /var/log/syslog
 

Sample outputs:

Apr 20 00:11:45 vivek-desktop kernel: [38600.515354] CPU0: Temperature/speed normal
Apr 20 00:12:20 vivek-desktop mysqld: Database Server failed

See howto write message to a syslog / log file for more information. Alternatively, you can see the logger man page for details:
man logger

#8: setterm Command

The setterm command can set various terminal attributes. In this example, force screen to turn black in 15 minutes. Monitor standby will occur at 60 minutes:

 
setterm -blank 15 -powersave powerdown -powerdown 60
 

In this example show underlined text for xterm window:

 
setterm -underline on;
echo "Add Your Important Message Here"
setterm -underline off
 

Another useful option is to turn on or off cursor:

 
setterm -cursor off
 

Turn it on:

 
setterm -cursor on
 

See the setterm command man page for details:
man setterm

#9: smbclient: Sending Messages To MS-Windows Workstations

The smbclient command can talk to an SMB/CIFS server. It can send a message to selected users or all users on MS-Windows systems:

smbclient -M WinXPPro <<EOF
Message 1
Message 2
...
..
EOF

OR

 
echo "${Message}" | smbclient -M salesguy2
 

See smbclient man page or read our previous post about "sending a message to Windows Workstation" with smbclient command:
man smbclient

#10: Bash Socket Programming

Under bash you can open a socket to pass some data through it. You don't have to use curl or lynx commands to just grab data from remote server. Bash comes with two special device files which can be used to open network sockets. From the bash man page:

  1. /dev/tcp/host/port - If host is a valid hostname or Internet address, and port is an integer port number or service name, bash attempts to open a TCP connection to the corresponding socket.
  2. /dev/udp/host/port - If host is a valid hostname or Internet address, and port is an integer port number or service name, bash attempts to open a UDP connection to the corresponding socket.

You can use this technquie to dermine if port is open or closed on local or remote server without using nmap or other port scanner:

# find out if TCP port 25 open or not
(echo >/dev/tcp/localhost/25) &>/dev/null && echo "TCP port 25 open" || echo "TCP port 25 close"

You can use bash loop and find out open ports with the snippets:

 
echo "Scanning TCP ports..."
for p in {1..1023}
do
  (echo >/dev/tcp/localhost/$p) >/dev/null 2>&1 && echo "$p open"
done

Sample outputs:

Scanning TCP ports...
22 open
53 open
80 open
139 open
445 open
631 open

In this example, your bash script act as an HTTP client:

#!/bin/bash
exec 3<> /dev/tcp/${1:-www.cyberciti.biz}/80
 
printf "GET / HTTP/1.0\r\n" >&3
printf "Accept: text/html, text/plain\r\n" >&3
printf "Accept-Language: en\r\n" >&3
printf "User-Agent: nixCraft_BashScript v.%s\r\n" "${BASH_VERSION}"   >&3
printf "\r\n" >&3
 
while read LINE <&3
do
   # do something on $LINE
   # or send $LINE to grep or awk for grabbing data
   # or simply display back data with echo command
   echo $LINE
done

See the bash man page for more information:
man bash

A Note About GUI Tools and Cronjob

You need to request local display/input service using export DISPLAY=[user's machine]:0 command if you are using cronjob to call your scripts. For example, call /home/vivek/scripts/monitor.stock.sh as follows which uses zenity tool:
@hourly DISPLAY=:0.0 /home/vivek/scripts/monitor.stock.sh

Have a favorite UNIX tool to spice up shell script? Share it in the comments below.

Featured Articles:

Share this with other sys admins!
Facebook it - Tweet it - Print it -

We're here to help you make the most of sysadmin work. So, subscribe!

{ 83 comments… read them below or add one }

1 Mike Williamson April 19, 2010

Awesome! Thanks for putting that together! Very useful stuff.

Reply

2 Marco Diego Aurélio Mesquita April 19, 2010

#10 does not work on ubuntu. :(

Reply

3 Sam Watkins April 20, 2010

Maybe you were using dash (Ubuntu’s /bin/sh) not bash. Try it with bash!
As for grep | awk, I like it. Pipelines with lots of simple components are fine, and it’s easy to understand. The person who stopped reading missed a great article.

Reply

4 Thanassis April 20, 2010

Output from “man bash | grep -A 12 tcp | head -11″:

/dev/tcp/host/port
If host is a valid hostname or Internet address, and port
is an integer port number or service name, bash attempts
to open a TCP connection to the corresponding socket.
/dev/udp/host/port
If host is a valid hostname or Internet address, and port
is an integer port number or service name, bash attempts
to open a UDP connection to the corresponding socket.

NOTE: Bash, as packaged for Debian, does not support using the /dev/tcp
and /dev/udp files.

So, as you see, Debian (and thus, Ubuntu) can’t make use of #10 …

Reply

5 Anonymous April 20, 2010

Ubuntu v9.10 user here :)

echo $BASH_VERSION
4.0.33(1)-release
lsb_release -d
Description: Ubuntu 9.10
echo >/dev/tcp/localhost/22 && echo "ssh open"
ssh open

Reply

6 Chris F.A. Johnson April 7, 2011

The Debian version of bash has the socket capability removed.

The Mandrive version has the builtin time command removed.

There may be other stupidites perormed by distros. That’s why I always build bash from source.

Reply

7 CC April 20, 2010

I stopped reading after #1. grep | awk is almost never required. Learn the tools before claiming proficiency.

Reply

8 Anonymous April 20, 2010

Grace us who are unworthy with your grepless/awkless version. I suppose you use perl, which is *never* required. Or do you apt-get some one-off program that just barley works on one particular Ubunt^W Linux distribution…

Reply

9 Eugéne April 21, 2010

CC was being ungracious, but to answer Anon’s question:

lynx –dump http://money.rediff.com/ | grep ‘BSE LIVE’| sed ‘s/^ *//’| tr -s ” “| cut -d” ” -f 5

would do the job just fine…

Reply

10 hwertz April 22, 2010

Sure it WOULD do the job fine. But it also is not particularly better than the initial version (also it still uses grep.) I don’t know what CC is complianing about, grep and awk are perhaps not REQUIRED, but so what? UNIX gives multiple ways to get almost anything done, so the programmer can chose the one they prefer. I see 0 problems with using grep & awk if the user desires. I must admit, Eugene, I like to use tr & cut too 8-).
Quite nice article!

Reply

11 Chris F.A. Johnson April 7, 2011

grep is not needed:

lynx -dump http://money.rediff.com/ | sed -n ‘/BSE LIVE/s/^ *//p’| tr -s “” | cut -d” ” -f 5

Reply

12 borzole April 22, 2010

Not quite. The idea of CC was that
grep ‘BSE LIVE’ | awk ‘{ print $5}’
could by writed using only awk:
awk ‘/BSE LIVE/{ print $5}’

BTW one more version:
lynx –dump http://money.rediff.com/ | awk ‘/BSE LIVE/{ print $5}’ | tr -d “,” | cut -d’.’ -f1

Reply

13 Vivek Gite April 22, 2010

You can short it further:

lynx --dump http://money.rediff.com/ | awk '/BSE LIVE/{ gsub(/,|\.[0-9]+/, ""); print $5}'

I can even ignore lynx and use bash sockets. There are multiple ways to solve the same problem.

Reply

14 borzole April 22, 2010

One more version. Maybe not so short, but in pure sed

#!/bin/bash
cat >cmd.sed<<__EOF__
/BSE LIVE/{
	s/,//g
	#   [20]Update Now BSE LIVE      17573.99     +101.43    +0.58%
	#^-------------1---------------^ ^-2-^.^-----------3-----------^
	s/\(.*\)\ \([0-9]*\)\.\([0-9]*\ .*\)/\2/g
	p
}
__EOF__
lynx --dump http://money.rediff.com/ | sed -n -f cmd.sed

Reply

15 Anonymous April 20, 2010

As a sysadmin of multiple architectures, portability is generally my main concern. Sure each shell has special features, but my scripts have to run in ksh, bash, and sh as required. Pipelines with grep/sed/awk make my job easier and allows my scripts run in more than just one brand of shell.

Reply

16 Joerg Paysen April 20, 2010

What is your alternative for #1 grep | awk ?

Reply

17 Anonymous April 20, 2010

Yeah, so what’s your version? Show me the money.

Reply

18 Anonymous April 20, 2010

awk ‘/BSE LIVE/{ print $5}’

Reply

19 Li chyun April 20, 2010

Thanks you stopped there — why dont you left w/o writing a comment.
It seems your only purpose is to write comment! God may forgive u r sins.

Reply

20 Sam Watkins April 20, 2010

thanks for the great article Vivek, I learned a lot from it! :)

Reply

21 steve April 20, 2010

Very nice list there. Do like the quick local open ports lister

Reply

22 MikeFM April 20, 2010

You’re biggest mistake, I think, is in assuming programs should communicate with end-users. I make scripts to do things I don’t want to have to deal with manually AT ALL. I don’t want to know when they run, what they’re doing, or if something went wrong. I want them to take care of it all for me and just keep things happy. If I personally was involved in what my scripts were doing I’d be wasting hours a day. If something really bad happens I’ll know because my system loggers will start bugging the crap out of me. (Really fun when a chain of errors spreads across many systems – oooh look 100,000 messages waiting in my inbox.)

Reply

23 roche April 24, 2010

Wait wait even Bash has builtin function in order to read variables from stdin.

Scripting language can be used for a wide range of uses even to interact with the users

Reply

24 Keilaron September 3, 2010

Your biggest mistake, I think, is assuming that all shell scripts are used solely for automated jobs ;)

Reply

25 Phil Ekstrom April 20, 2010

Great stuff. I am not quite yet using *nix, so wanted to download the PDF version. The attempt got me a 404 error.

Reply

26 Vivek Gite April 21, 2010

Sorry about 404. It is fixed now.

Reply

27 Jay April 20, 2010

Basically a Linux list for the most part, not unix. /bin/bash gives it away (and the bashisms in the zenity example), also *bash* doesn’t come with /dev/{tcp,udp}, that is Linux.

Reply

28 Thanassis April 20, 2010

Wrong, Jay.
A simple “man bash” followed by /tcp shows it. Unfortunately, it doesn’t work under Debian/Ubuntu, since this functionality is disabled by the Debian maintainer of bash.

Reply

29 lowell April 20, 2010

Kind of annoyed that of the 9 utilities listed, only 3 of them come with Mac OS X, which, unlike your test systems, is an actual UNIX-certified system. In other words, it was implied that these were standardized utilities, when they’re not. Oh well, I’m sure I can still find them if I really need them. =/

Reply

30 MikeFM April 20, 2010

Unfortunately Mac OS runs a nasty crippled version of Unix instead of Linux inside. Is highly frustrating sometimes. At least it isn’t as retarded as trying to use AIX. Using AIX makes me want to install Linux really REALLY badly but sadly it’s required for the service I run on it. I always wonder what is wrong with these other Unix platforms that they haven’t adopted all the improvements that have made it into Linux and the GNU tools.

Reply

31 frishrash April 20, 2010

Mike, AIX is the most powerful unix platform I’ve ever worked with. I don’t want to get into semi-religious arguments, and I like linux too, but when it comes to servers stability, hardware management, workload management, etc.. it’s leading operating system if not the best.

Sure, it lacks some of the GNU tools by default but you can find almost everything you need on AIX Linux Toolbox CD. Anyway I think AIX is mainly used in servers markets, so desktop utilities are usually irrelevant.

And I’m not working for IBM, nor got any shares :)

Reply

32 od April 20, 2010

Aww.. why don’t you go home and play with your penguin doll before the red demon starts poking you with pitchfork.

Reply

33 Leaman Crews April 20, 2010

Mac OS X is missing a lot of commands you would expect to find in both Linux and *BSD out of the box. watch is the one I miss the most when working on OS X.

Fact of the matter is that OS X is Unix certified (with 10.5 or higher on x86 hardware ONLY), but what does that really mean if the tools you expect from a standard *nix distribution aren’t there by default? (And yes, I know how to get them all with fink, macports or compiling them myself.)

Reply

34 jason December 7, 2011

hey, AIX is awesome.. runs rings around any other OS… try and find me a OS where you can dynamically add and remove CPU and memory and storage on the fly without having to stop/start apps. And the hardware is awesome.
And ksh is as good as any other shell, it has internal arithmatic and can perform nested loops.
I’d like to hear you come up with a better real UNIX OS that can do what AIX and pSeries hardware can do…

Oh, BTW, I liked this article.

Reply

35 jack April 20, 2010

Really good list. I’m going to bookmark this one. Thanks a lot.

Reply

36 Gaurish April 20, 2010

Does notify-send work with KDE4?

Reply

37 cipper April 20, 2010

[QUOTE]Does notify-send work with KDE4?[\QUOTE]
use this:
kdialog –passivepopup “HI!” 3

Reply

38 frishrash April 20, 2010

“A Note About GUI Tools and Cronjob” – In order to run zenity from cron, at least under Ubuntu, setting DISPLAY didn’t work for me, I had to set LANG as well…

The list is nice indeed. I would’ve added bash built-in select command as well.

Reply

39 John-John Tedro April 20, 2010

Notify OSD (the daemon) actually ignores the timeout:
https://bugs.launchpad.net/ubuntu/+source/notify-osd/+bug/390508

Idea that sums it up:
http://brainstorm.ubuntu.com/idea/24001/ (see how the notify-osd devs just ignores the idea?)

Just a side note, been some source of frustration from my part seeing as I always have to patch notify-osd on my development machines.

Reply

40 Alfonso de Cala April 20, 2010

Great article!

I miss a really useful command from 4DOS in linux: “select”

With “select” you could see a (curses) list of files, select some of them and run a command using the list as parameter.

Perhaps this feature exists, but I haven’t found it yet.

Reply

41 Mustafa April 20, 2010

some guys are just suckers. someone has made an effort and pointing us in the right direction which can open up a new world of possibilities. But some cold stiff upper lipped people just want to play down the efforts. if you cant do it better, then just shut up ! kudos to vivek for putting in the effort.

Reply

42 Steven April 21, 2010

My thoughts exactly! Why are these gurus wasting their time here in stead of creating a blog of their own and putting their link up here to share their vast knowledge with the rest of the plebs.

Vivek must have been reading my thoughts, I was thinking about this this week, made my life a lot easier… Esp. the KDE link opened up a huge list of possibilities, so it seems.

Thanks Vivek!

Reply

43 Bob April 20, 2010

The setleds command only works for console sessions. It doesn’t work from gnome-terminal. Who uses console sessions these days?

Reply

44 Zukero April 20, 2010

I do a lot these days, while creating a secure X terminal distro. My only entry point to the terminal’s OS is with the tty1-6.

Reply

45 verboze April 20, 2010

My guess is probably about 99% of sys admins out there who need to manage multiple *NIX servers and do not want to do so by logging in via a GUI in every single machine? Just a thought.

Reply

46 Vivek Gite April 20, 2010

You can setup a GUI workstation (say CentOS with Gnome) and write a small script app that allows you to select remote server and run bunch of commands (on selected remote server) to find uptime, top memory consuming process, real time logs and much more.

Reply

47 Anonymous April 20, 2010

Everybody maintaining remote servers through ssh (Yes I know you can tunel VNC through ssh, but seriously get a bad link and its painful) + some server distros don’t even come with a default x session. generally system admisitration is MUCH quicker from the console if you know what your doing.

Reply

48 Steven April 21, 2010

A lot of people that are taking their first steps in the Linux world are using the GUI.

This is something I observed for myself. I am also doing more and more through the console, but it took me a few years to get there.

As for remote X sessions, I find NX very useful and very performant, even using slow links. All it takes is installing the software, enter one config command, DL the client, and you’re good to go. It uses SSH as transport medium, so you can use all the goodies that SSH provides out-of-the-box.

Reply

49 krishna April 20, 2010

Ohh cool one ..i will start spicing my scripts now :) thanks

Reply

50 Igor April 20, 2010

Great article Vivek, a lot of interesting ideas and starting points. Just a few words to say I appreciated your work…

Reply

51 Drew April 20, 2010

I created a simple util that graphs a stream of numbers using a simple cursors based script (python that calls tput). awk/perl/etc.. a column and either tail to watch the current state, or cat a whole file to see a historical view.

Link [code.activestate.com]

Reply

52 jaysunn April 20, 2010

@Vivek,

Very intersting post. I have been using some of those tools already but not in the ways you have demonstated.

I am very fond of:
#4: zenity Command

Thanks.
jaysunn

Reply

53 Esneil April 20, 2010

Thank you very much…..This is the reason why linux is the best OS…

Reply

54 kashyap April 20, 2010

for #10 can we use this script to scan a remote host. if so what are the changes we need to make.bcoz this quite a good way and faster one!!!

good idea vivek!!

Reply

55 Vivek Gite April 20, 2010

Replace localhost with remote server name.

Reply

56 Ulver April 20, 2010

@Vivek, awesome compilation !

Reply

57 Vamsi April 20, 2010

wow :D just wow !

Reply

58 Linux beginner April 20, 2010

Hi,
Great article. Combining CLI with GUI is really usefull.
Thanks

Reply

59 Kimura April 20, 2010

Thank for the tips, :)

Reply

60 Shantanu Oak April 21, 2010

notify-send and logger are the 2 commands I have already been using in almost every script. Thanks for other commands.

Reply

61 !ncognito April 21, 2010

Awesome! Very useful.

Reply

62 mario April 21, 2010

I do not like “zenity”. For the sole reason that it breaks compatibility to *all* the other dialog utilitys. Yes, kdialog, gdialog, xdialog, dialog & whiptail all share the same basic commandline switches. Only the zenity developers choose to break the standard. Let’s remind ourselves that destandardization is never a good thing; as open source users we should never forget that.

Reply

63 Chr1573r April 21, 2010

Thanks for all these handy tips!
#10 is great for fast security audits!

Reply

64 Matt E. April 22, 2010

Great article, Vivek! And timely, too. I don’t get a chance to script often in my position, but I just began working on a account management menu for another group to use. A lot of these tips will get some use from me.

Thanks again!

Reply

65 kumarat9pm April 22, 2010

Really awesome man.. will try some of them.. thanks for sharing..

Reply

66 paurullan April 24, 2010

You forgot «beep» :D

Reply

67 Keilaron September 3, 2010

I once wrote a bash script that would parse old BBS-style “music” scripts (used by a certain portal or pager software I think) and play them using beep. I then used it for paging me when long jobs were done or someone wanted my attention.

Reply

68 Howard H April 24, 2010

To CC,
actually grep | awk | sed can be rolled up into a single command…
awk '/BSE LIVE/ { gsub(",", "", $5); gsug("\.[0-9]", "", $5); print $5}'
or this…
sed -n -e '/BSE LIVE/{s/.* .* \(.*\)\.[0-9]*/\1/;s/,//gp}'

Reply

69 Anonymous April 24, 2010

As posted earlier in thread one gsub is sufficient ;)
gsub(/,|\.[0-9]+/, "");

Reply

70 Sam Watkins April 27, 2010

I think the original:
grep ‘BSE LIVE’ | awk ‘{ print $5}’ | sed ‘s/,//g;s/\.[0-9]*//g’
is shorter and more intelligible. Plain awk is okay too, but please, not sed for this!
I’m a fan of highly pipelined parallel processing, with very simple components. It’s a good way to program, although unix might not be able to implement this super-efficiently, and the shell is not the best possible language for it.

Reply

71 Jay April 25, 2010

Very nice guide. You might like to know that in the notify-send example with the -i flag, the gtk-dialog-info parameter is missing (but is still explained below).

Reply

72 greg April 26, 2010

Great job! Nice to have it in one place.

Reply

73 Patola April 29, 2010

For zsh lovers out there,

Even though zsh does not have the /dev/tcp fake device file, it has a similar feature through a loadable module zsh/net/tcp. You can use “autoload -U tcp_open” and then consult the manual page – “man zshtcpsys” – for details on this very powerful subsystem. You can handle the tcp connections in several ways, even using a file descriptor, or alternatively with the commands tcp_read and tcp_send and then tcp_close. You can also use “tcp_expect” for expect-like functionality.

Reply

74 Holiday with dogs May 19, 2010

Even though zsh does not have the /dev/tcp fake device file, it has a similar feature through a loadable module zsh/net/tcp. You can use “autoload -U tcp_open” and then consult the manual page – “man zshtcpsys” – for details on this very powerful subsystem. You can handle the tcp connections in several ways, even using a file descriptor, or alternatively with the commands tcp_read and tcp_send and then tcp_close. You can also use “tcp_expect” for expect-like functionality.
+1

Reply

75 AsTeR May 20, 2010

Sorry, it’s in french but here is a Zenity Generator, very useful for your scripts : http://doc.ubuntu-fr.org/zenitor

Reply

76 Student Brands May 26, 2010

Cool, thanks :)

Reply

77 shulato June 12, 2010

awesome! nice tricks!

Reply

78 Rameshkumar July 13, 2010

Thanks for the great post, Vivek. I have learned a lot Today.

Reply

79 Jagnikam July 14, 2010

Great Article…… Thanks Vivek

Reply

80 unoduetre August 29, 2010

#11
Do you know how to open bidirectional channel to any device from a terminal?
Just use socat utility. With it’s help you can communicate with (for example) your printer and send it PostScript directly, to print white or black pages for example.

Reply

81 Ikem August 31, 2010
82 mightyuhu August 8, 2011

Great Article, this commands have been very useful.

Reply

83 Jayaram prasad December 9, 2011

really useful commands ……

Thanx friends…..

Reply

Leave a Comment

You can use these HTML tags and attributes for your code and commands: <strong> <em> <ol> <li> <u> <ul> <blockquote> <pre> <a href="" title="">
What is 6 + 12 ?
Please leave these two fields as-is:
Are you a human being? Solve the simple math so we know that you are a human and not a bot.




Previous post:

Next post: