In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >
Share
Shulou(Shulou.com)06/01 Report--
Summary of grammar knowledge points of bash script programming the second part
Undertake the first part of the summary and analysis.
Interactive programming of 6.bash programming
Read
-p "prompt" / / prompt
-ttimeout
Give a default value to a variable
VarName=$ {varName:-value}
If varName is not empty, the value of varName is returned; otherwise varName will use value as its value
Using the read parameter [- p] allows [- p] to be followed by a string and n shell variables after the string. N shell variables are used to receive strings input from the shell interface
Usage: read-p "string" var1 var2... Varn
Exercise: determine the content type of a file by giving the path to a file on the keyboard
#! / bin/bash
Read-p "Enter a file name:" fileName
Type $fileName
The content type of the file under the directory is determined by a path given by the keyboard, which defaults to /.
For example, enter a user name and return its shell
#! / bin/bash
#
Read-p "Plz input a username:" userName
Ifid $userName & > / dev/null; then
Echo "The shell of $userName is `grep" ^ $userName\ > "/ etc/passwd | cut-d:-f7`."
Else
Echo "No such user. Stupid."
Fi
Example: a menu is displayed to the user as follows:
Cpu) show cpu infomation
Mem) show memory infomation
*) quit
1. If the user selects cpu, the contents of the / proc/cpuinfo file are displayed
2. If the user selects mem, the contents of the / proc/meminfo file are displayed
3. Quit
#! / bin/bash
#
Echo "- menu-"
Echo "cpu) show cpu infomation"
Echo "mem) show memory infomation"
Echo "*) quit"
Echo "- menu-"
Read-p "Plz give your choice:" choice
If ["$choice" = = 'cpu']; then
Cat / proc/cpuinfo
Elif ["$choice" = = 'mem']; then
Cat / proc/meminfo
Else
Echo "Quit"
Exit 3
Fi
No newlines, tabs, or spaces before the second EOF
#! / bin/bash
#
Cat / dev/null
[$?-eq 0] & & echo "Stop $srvName OK" & & return 0
Else
Echo "$srvName is not started."
Return 1
Fi
}
Status () {
If [- f $lockFile]; then
Echo "$srvName is running."
Else
Echo "$srvName is stopped."
Fi
Return 0
}
Usage () {
Echo "Usage:$srvName {start | stop | restart | status}"
Return 0
}
Case $1 in
Start)
Start
Stop)
Stop
Restart)
Stop
Start
Status)
Status
*)
Usage
Exit 1
Esac
Exercise: write a script to perform the following functions (using functions):
1. Prompt the user for an executable command
2. Get all the library files on which this command depends (using the ldd command)
3. Copy the command to the directory corresponding to / mnt/sysroot/
Explanation: suppose that if you copy the cat command and its executable path is / bin/cat, then copy / bin/cat to the / mnt/sysroot/bin/ directory. If you copy the useradd command and the executable path of useradd is / usr/sbin/useradd, copy it to the / mnt/sysroot/usr/sbin/ directory.
4. Copy the library files to the corresponding directory of / mnt/sysroot/
#! / bin/bash
#
Target=/mnt/sysroot/
[- d $target] | | mkdir $target
PreCommand () {
If which $1 & > / dev/null; then
CommandPath= `which-- skip-alias $1`
Return0
Else
Echo "No such command."
Return1
Fi
}
CommandCopy () {
CommandDir= `dirname $1`
[- d ${target} ${commandDir}] | | mkdir-p ${target} ${commandDir}
[- f ${target} ${commandPath}] | | cp $1 ${target} ${commandDir}
}
LibCopy () {
For lib in `ldd $1 | egrep-o "/ [^ [: space:]] +" `; do
LibDir= `dirname $lib`
[- d$ {target} ${libDir}] | | mkdir-p ${target} ${libDir}
[- f ${target} ${lib}] | | cp $lib ${target} ${libDir}
Done
}
Read-p "Plz enter a command:" command
Until ["$command" = = 'quit']; do
If preCommand $command & > / dev/null; then
CommandCopy $commandPath
LibCopy $commandPath
Fi
Read-p "Plz enter a command:" command
Done
Signal capture in 8.bash programming
Signal type
1) SIGHUP this signal is sent at the end of the user terminal connection (normal or abnormal), usually when the terminal control process ends, notifying each job in the same session, then they are no longer associated with the control terminal.
2) SIGINT program termination (interrupt) signal, which is sent when the user types an INTR character (usually Ctrl-C)
3) SIGQUIT is similar to SIGINT, but is controlled by QUIT characters (usually Ctrl-/). The process generates a core file when it receives a SIGQUIT exit, which in this sense 1 is similar to a program error signal.
4) SIGILL executed illegal instructions, usually because of an error in the executable itself or an attempt to execute a data segment. This signal may also be generated when the stack is overflowed.
The trap command is used to capture a signal in a shell program, after which you can react in three ways:
(1) execute a program to process the signal
(2) default operation of accepting signals
(3) ignore this signal
Trap provides three basic forms for the above three ways:
The first form of trap command executes the command string in double quotes when shell receives a signal with the same value in the signallist list.
Trap 'commands' signal-list
Trap "commands" signal-list
The second form of trap command restores the default operation of the signal: trap signal-list
The third form of trap command allows you to ignore the signal: trap "" signal-list
Trap 'COMMAND' SIGINT (for shutting down the process)
Example: write a script that can ping to detect whether all hosts in the specified network are online, and can receive ctrl+c commands to exit when the execution is not finished.
#! / bin/bash
QuitScript () {
Echo "Quit..."
}
Trap 'quitScript; exit 5' SIGINT
CnetPing () {
For i in {1... 254}; do
If ping-c 1-W 1 $1.Secreti & > / dev/null; then
Echo "$1.Secreti is up."
Else
Echo "$1.Secreti is down."
Fi
Done
}
BnetPing () {
For j in {0.255}; do
CnetPing $1.10j
Done
}
AnetPing () {
For m in {0.255}; do
BnetPing $1.millim
Done
}
NetType= `echo $1 | cut-d "."-f1`
If [$netType-ge 1-a $netType-le 126]; then
AnetPing $netType
Elif [$netType-ge 128-a $netType-le 191]; then
BnetPing $(echo $1 | cut-dwelling.'- F1 Magi 2)
Elif [$netType-ge 192-a $netType-le 223]; then
CnetPing $(echo $1 | cut-dudes.'- F1-3)
Else
Echo "Wrong"
Exit 2
Fi
Array 9.1 Array definition for 9.bash programming
Array: multiple consecutive independent memory spaces, each equivalent to a variable
Array elements: array name + index (numbered from 0)
The representation of the index:
Digital index: a [index]
A [0], a [1]
Associative array of bash 4.0s
A [hello], a [hi]
Declaration array: declare-an ARRAR_NAME
Associative array: declare-An ARRAY_NAME
Support for sparse formats: only one-dimensional arrays
Array assignment:
(1) assign values to one element at a time:
A [0] = $RANDOM
...
Echo ${a [0]}
(2) assign values to all elements at once:
A = (red blue yellowgreen)
A pair of parentheses indicates an array, and the array elements are separated by a "space" symbol.
(3) assign values by index:
A = ([0] = green [3] = red [2] = blue [6] = yellow)
(4) replace the command:
Logs= ($(ls / var/log/*.log))
Or logs= (/ var/log/*.log)
Echo ${logs [0]}
(5) user input
Read-an ARRAY
Array access:
Access with index: array [index]
Length of the array:
${# ARRAY [*]}
${# ARRAY [@]}
Eg:echo ${# test [*]}
Echo ${# test [@]}
Exercise: write a script to generate 10 random numbers, save them to an array, and then display the elements marked as even in the array
#! / bin/bash
For i in {0.. 9}; do
Num [$I] = $RANDOM
If [$[$I% 2]-eq 0]; then
Echo "a [I] is$ {a [I]}"
Fi
Done
To select an element from an array:
${ARRAY [@]: offset:number}
Slice:
Offset: number of elements offset
Number: the number of elements taken out
${ARRAY [@]: offset}: all elements after the offset is taken out
${ARRAY [@]}: take out all elements
Array replication:
To use ${ARRAY [@]}
$@: each parameter is a separate string
$*: all parameters are a string
Append elements to an array: non-sparse format
Mylogs
Mylogs [${# week [@]}]
Example: copy the even-numbered elements of an array to a new array
#! / bin/bash
Declare-a mylogs
Logs= (/ var/log/*.log)
Echo ${logs [@]}
For i in `seq 0 ${# logs [@]} `; do
If [$[$I% 2]-eq 0]; then
Index=$ {# mylogs [@]}
Mylogs [$index] = ${logs [$I]}
Fi
Done
Echo ${mylogs [@]}
Remove the element from the array: unset ARRAY [index]
Exercise 1: generate 10 random numbers and sort them in ascending order
#! / bin/bash
For ((ionometry1))
Do
For ((jumb0witj
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.