`

10 Tools To Add Some Spice To Your UNIX Shell Scripts

阅读更多

 

T here 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.

分享到:
评论

相关推荐

    spice-guest-tools-0.74

    spice-guest-tools-0.74

    spice-guest-tools-0.164.3.iso

    Arm 版 MacOS 安装 Windows 11 就这么简单 SPICE 项目提供了几个关键组件: SPICE 协议:SPICE 协议定义了虚拟机和客户端之间的通信规范,包括图形、音频、输入...SPICE Guest Tools spice-guest-tools-0.164.3.iso

    spice-guest-tools-2021.zip

    - Spice Guest Tools通常适用于各种操作系统,如Windows、Linux和某些Unix变体。 - 确保虚拟机的软件和硬件配置符合最低系统要求,以保证顺利安装和运行。 通过安装和使用Spice Guest Tools,用户可以在虚拟环境...

    spice-guest-tools-0.74.zip

    "spice-guest-tools-0.74.zip"是一个压缩包文件,包含了针对KVM(Kernel-based Virtual Machine)环境的虚拟机客座工具,主要服务于Windows操作系统。这些工具旨在优化和增强虚拟化环境中的用户体验,特别是针对使用...

    Designers-Guide_to_Spice_and_Spectre总结文档

    《Designers-Guide_to_Spice_and_Spectre总结文档》是1995年出版的一本关于Spice和Spectre仿真的指南书籍,作者是Ken Kundert。本书为电子电路设计师提供了一个深入了解电路仿真软件如何工作以及如何设置仿真的宝贵...

    Designers-Guide to Spice and Spectre.pdf

    《Designers-Guide to Spice and Spectre》是一本由肯·昆德特(Ken Kundert)于1995年撰写的关于Spice和Spectre仿真的权威指南。这本书经过许建超翻译后于2008年由西安交通大学出版。Spice是一款广泛使用的电路仿真...

    spice-guest-tools-0.164.4

    用于mac m系列芯片用UTM安装虚拟机后,解决无法调整分辨率的问题

    Introduction to Analog “SPICE” Simulation

    模拟“SPICE”仿真简介 SPICE(Simulation Program with Integrated Circuit Emphasis)是一种用于电路仿真、特别是集成电路仿真的软件程序。该程序最初于20世纪70年代在加州大学伯克利分校开发,目的是为了辅助...

    Spice用户指南和参考

    The aim in Chapter 2 is just to help you write, run, and understand your first Spice file. In Chapter 3, each of the major types of analyses Spice can do for you are introduced, by example. In ...

    The_Designer's_Guide_to_SPICE

    The_Designer's_Guide_to_SPICE,_SPICE学习必备。

    spice-guest-tools-latest(包含agent和qxl驱动).exe

    用于虚拟机磁盘转换,可能把hyper-v虚拟机磁盘转换成KVM虚拟机磁盘,实现hyper-v虚拟机迁移到KVM平台下面。完美解决hyper-v虚拟机转换变KVM虚拟机。

    IBIS2SPICE

    The IBIS to SPICE application generates SPICE models from IBIS (I/O Buffer Information Specification) data sheets. It reads a template file to define the SPICE models to be generate. The template file...

    spice2.unix.tar.gz

    **SPICE2G6:电路模拟的基石** SPICE(Simulation Program with Integrated Circuit Emphasis)是一种广泛用于模拟和分析电子电路的开源软件。SPICE2G6是SPICE的早期版本,它为后来的电路仿真工具,如Pspice,奠定...

    SPICE A Guide to Circuit Simulation and Analysis using pspice.pdf

    SPICE A Guide to Circuit Simulation and Analysis using pspice.pdf

    手把手教你安装配置spice服务

    Spice(Simple Protocol for Independent Computing Environments)是一种远程桌面协议,设计用于提供高效、低延迟的虚拟机(VM)图形用户界面(GUI)访问。它广泛应用于OpenStack这样的开源云计算平台,为用户提供...

    spice-0.12.2

    SPICE is a remote display system built for virtual environments which allows you to view a computing 'desktop' environment not only on the machine where it is running, but from anywhere on the ...

    开源虚拟桌面传输协议-spice用户指导手册

    SPICE(Simple Protocol for Independent Computing Environments,独立计算环境的简单协议)是一种开源虚拟桌面传输协议,旨在帮助用户通过服务器和远程计算机(包括桌面和瘦客户端设备)部署虚拟桌面。SPICE用户...

    SPICE模型参数介绍

    SPICE模型参数是电路仿真软件SPICE(Simulation Program with Integrated Circuit Emphasis)中用于定义电路元件特性的关键数据。SPICE模型参数使设计人员能够在计算机上对电路进行仿真,以此来预测电路在不同条件下...

Global site tag (gtag.js) - Google Analytics