首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 操作系统 > UNIXLINUX >

十 Tools To Add Some Spice To Your UNIX Shell Scripts

2012-07-22 
10 Tools To Add Some Spice To Your UNIX Shell Scripts?Creating GUI application is not just expensiv

10 Tools To Add Some Spice To Your UNIX 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:

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/bashdialog --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 scriptspython-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/messageslogger -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 normalApr 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 <<EOFMessage 1Message 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:

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

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 open53 open80 open139 open445 open631 open

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

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

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.

热点排行