1. Script vim environment In the script, you usually need to display some script information. This information can be automatically displayed by setting vim The meaning of the configuration information: set nu shows line number set nu ts=2 et ai autocmd BufNewFile *.sh,*.script call SHELLTITLE() func SHELLTITLE() call append(0,"#####################################") call append(1,"# Author: lee") call append(2,"# Version: 1.0") call append(3,"# Create_Time: ".strftime("%Y/%m/%d")) call append(4,"# Mail: [email protected]") call append(5,"# Info: ") call append(6,"#") call append(7,"#") call append(8,"#####################################") call append(9,"") call append(10,"#!/bin/bash") endfunc As shown in the figure: Scripting Exercise: #!/bin/bash [ -z "$1" ] && { echo "Error : Please input interface following script !!" exit } ifconfig $1 | awk '/\<inet\>/{print $2}' The effect is as shown below: (2). Create host_messages.sh to display the name of the current host and the IP address of the user who logged in to the current host. #!/bin/bash echo "hostname: `hostname`" echo "ipaddress: `ifconfig ens3 | awk '/\<inet\>/{print $2}'`" echo "username: $USER" !!!Note the difference between single quotes and back quotes. Single quotes '' are weak quotes and cannot quote \ ` $ !; Back quotes `` are strong quotes and can quote all (3) Create clear_log.sh to execute the script to clear the log #!/bin/bash [ "$USER" != "root" ] && { echo "Please run $0 with root !!!" exit } [ ! -e "/var/log/messages" ] && { echo "No logfile here!!!" exit } > /var/log/messages && { echo logfile has been cleared !!! exit } 2. How to define the environment in shell scripts Environmental Level When the variable value specified by the shell command export a=1 is used, it cannot be recognized in the script because the two are not opened in the same shell. 3. Translated characters in shell scripts Translate a single character: \ 4. Arrays of variables in scripts a=(1 2 3 4 5) 5. Alias settings for system commandsalias xie='vim' # Temporary setting to switch off shell failure vim ~/.bashrc #User-level configuration file vim /etc/bashrc #system level To remove an alias: 6. Passing parameters in scripts Non-interactive mode: Interactive Mode: #!/bin/bash read -p "Pleas input word:" WORD echo $WORD The effect is as shown below: -s means hiding the entered WORD #!/bin/bash read -p "Pleas input word:" -s WORD echo $WORD There will be hidden effects when entering. As shown in the figure: Scripting Exercise: #!/bin/bash [ -z $1 ] && { echo "Please input username: !!" exit } id $1 &> /dev/null && { echo "$1 is exit !!" }||{ useradd $1 &> /dev/null read -p "Please input user's password: " -s PASS echo " " echo $PASS |passwd --stdin $1 &> /dev/null && { echo "user is created success !!" } } The effect is as shown below: 7. Loop function in script#!/bin/bash ECHO() { [ "$WORD" = "exit" -o "$WORD" = "EXIT" ] && { echo bye exit } read -p "Please input word:" WORD echo $WORD ECHO } ECHO Functions can make script actions execute in a loop. The effect is as shown below: Scripting Exercise: #!/bin/bash FILE() { [ $1 "$FILENAME" ] && { echo $FILENAME is "$2" echo "" check } } check() { read -p "Please input filename :" FILENAME [ "$FILENAME" = "exit" ] && { echo bye exit } FILE "! -e" "not found" FILE -L "link file" FILE -f "common file" FILE -d "directory" FILE -S "socket" FILE -b "block file" FILE -c "cahr file" check } check The effect is as shown below: Script Exercise 2: #!/bin/bash Create_user() { read -p "Please input username : " USER echo "" [ "$USER" = "exit" ] && { echo bye exit } id $USER &> /dev/null && { echo "$USER is exit !!" Create_user }||{ useradd $USER read -p "Please input password :" -s PASS echo "" echo $PASS |passwd --stdin $USER &> /dev/null && { echo "" echo $USER is created !! } } Create_user } create_user The effect is as shown below: This is the end of this article about the detailed introduction of Linux script basics. For more relevant Linux script basics content, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: Detailed explanation of the causes and solutions of conflicts between filters and fixed
>>: MySQL Series 10 MySQL Transaction Isolation to Implement Concurrency Control
Rendering Code - Take the blue and yellow rings a...
View the engines supported by the current databas...
In the horizontal direction, you can set the alig...
Find mirror We can search for images from the Doc...
Table of contents redo log Why do we need to upda...
Preface I have been working on some front-end pro...
1. MySQL rpm package installation # Download the ...
Table of contents What is front-end routing? How ...
In MySQL, you can use IF(), IFNULL(), NULLIF(), a...
A simple license plate input component (vue) for ...
Table of contents Preface What is Hot Change Coco...
"Page screenshot" is a requirement ofte...
This article example shares the specific code of ...
Mongodb has a db.serverStatus() command, which ca...
The previous article explained how to reset the M...