Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the 10 tools that make UNIX/Linux Shell scripts interesting?

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article shows you 10 tools to make UNIX/Linux Shell scripts interesting, which are concise and easy to understand, which will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

There is some misconception that shell scripts are only used in CLI environments. In fact, under the KDE or Gnome desktop, you can effectively use a variety of tools to write GUI or socket scripts. Shell scripts can use some GUI components (menus, warning boxes, progress bars, etc.), you can control the terminal output, cursor position, various output effects, and so on. With the following tools, you can build strong, interactive, and user-friendly UNIX/Linux bash scripts.

Making GUI applications is not a difficult task, but it takes time and patience. Fortunately, both UNIX and Linux come with a lot of tools for writing beautiful GUI scripts. The following tools are tested based on FreeBSD and Linux operating systems, and are also suitable for other UNIX operating systems.

1. Notify-send command

The notify-send command allows you to send desktop notifications to users with the help of notification daemons. This way to avoid disturbing the user is useful for notifying the desktop user of an event or displaying some information. On Debian or Ubuntu, you need to install the package using the apt command or the apt-get command:

Sudo apt-get install libnotify-bin

The CentOS/RHEL user uses the following yum command:

Sudo yum install libnotify

The Fedora Linux user uses the following dnf command:

`$ sudo dnf install libnotify`in this example, send simple desktop notification from the command line, enter:### send some notifications # notify-send "rsnapshot done:)"

Sample output:

Notify-send: Shell Script Get Or Send Desktop Notifications

Here is the code for another additional option:

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

Sample output:

Linux / UNIX: Display Notifications From Your Shell Scripts With notify-send

Here:

-t 5000: specify timeout (milliseconds) (5000 milliseconds = 5 seconds)

-u low: sets the emergency level (e.g., low, normal, emergency)

-I gtk-dialog-info: sets the name of the icon to display or the specified icon (you can set the path to:-I / path/to/your-icon.png)

For more information about using notify-send features, please refer to the man manual. Enter man notify-send at the command line and you will see:

Man notify-send2, tput command

The tput command sets terminal properties. Through tput, you can set:

Move the cursor on the screen.

Get terminal information.

Set the color (background and foreground).

Sets the bold mode.

Set the reverse mode and so on.

Here is a sample code:

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

Sample output:

Linux / UNIX Script Colours and Cursor Movement With tput

For more information about the tput command, see the manual:

Man 5 terminfoman tput3, setleds command

The setleds command allows you to set keyboard lights. The following is an example of turning on a numeric key light:

Setleds-D + num

Turn off the digital key light and enter:

Setleds-D-num

-caps: turn off the case lock light

+ caps: turn on the case lock light

-scroll: turn off the scroll lock light

+ scroll: turn on the scroll lock light

See the setleds manual for more information and options man setleds.

4. Zenity command

The zenity command displays the GTK+ dialog box and returns user input. It allows you to use various Shell scripts to display or request information to users. The following is an example of a GUI client for a whois specified domain name directory service.

#! / 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 output:

Zenity: Linux / UNIX display Dialogs Boxes From The Shell Scripts

Components of GTK+:

Zenity-- helpman zenity5, kdialog command

The kdialog command is similar to zenity, but it is designed for KDE desktops and QT applications. You can use the kdialog display dialog box. The following example displays information on the screen:

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

Sample output:

Kdialog: Suppressing the display of a dialog

6 、 Dialog

Dialog is an application that uses Shell scripts to display the text of user interface components. It uses curses or ncurses libraries. 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 manual for more information: man dialog.

Considerations for other user interface tools

UNIX and Linux provide a number of other tools to display and control applications on the command line, and shell scripts can use some sets of KDE, Gnome, and X components:

Gmessage-GTK xmessage-based cloning

Xmessage-displays or asks for messages in the window (X-based / bin/echo)

Whiptail-displays a dialog box from a shell script

Python-dialog-Python module for making a simple text or console mode user interface

7. Logger command

The logger command writes information to a system log file, such as: / var/log/messages. It provides a shell command line interface for the Syslog module syslog:

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

Sample output:

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 "how to write messages to syslog or log files" for more information. In addition, you can also check the logger manual for more information: man logger

8. Setterm command

The setterm command sets different terminal properties. The following example code forces the screen to turn black after 15 minutes and the monitor to stand by after 60 minutes.

Setterm-blank 15-powersave powerdown-powerdown 60

The following example shows the underlining of the text in the xterm window:

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

Another useful option is to turn the cursor display on or off:

Setterm-cursor off

Open the cursor:

Setterm-cursor on

See the setterm command manual for more information: man setterm

9. Smbclient: send messages to MS-Windows workstations

The smbclient command can communicate with the SMB/CIFS server. It can send messages to selected or all users on the MS-Windows system.

Smbclient-M WinXPPro / dev/null & & echo "TCP port 25 open" | | echo "TCP port 25 close"

In the following code snippet, you can use the bash loop to find the open port:

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

Sample output:

Scanning TCP ports...22 open53 open80 open139 open445 open631 open

In the following example, your bash script will work like a 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

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report