Bash knowledge points for file testing:
Knowledge Review:
Character test:
Format: Test expression
: [Expression]
: [[Expression]]
Integer test:
Format: Test expression
: [Expression]
: [[Expression]]
File test:
Monocular test:
-e/path/to/file: Test file exists
-a/path/to/file: Test file exists
-f/path/to/file: Determine if the file is a normal file
-d/path/to/file: Determine if the file is a directory file
-b/path/to/file: Determine if the file is a block device file
-c/path/to/file: Determine if the file is a character device file
-H or-l/path/to/file: Determine if the file is a symbolic link file
-p/path/to/file: Determine if the file is a pipe file
-s/path/to/file: Determine if a file exists and is a socket file
-r/path/to/file: Determines whether the current file exists and whether the current user has readable permissions on the file
-w/path/to/file: Determines whether the current file exists and whether the current user has write access to the file
-x/path/to/file: Determines whether the current file exists and whether the current user has execute permissions on the file
-s/path/to/file: Determine if the current file exists and its contents are empty
Binocular test:
File1-nt file2: File 1 is newer than file 2 (modification time), if file 1 exists, file 2 does not exist, also think File1 update
File1-ot file2: File 1 is older than file 2
File1-ef file2: Does file 1 and file 2 refer to the same device file
Example 1: Create the file if/TMP/HELLO.R does not exist
#!/bin/bash
Filename=/tmp/hello.r
[-E $fileName]| | mkdir $fileName
Example 2: If the current file has read access to the profile, we will load it into
In the script, the method of loading the configuration in another file
Method 1:source/path/to/somefile or. /path/to/somefile
The contents of the configuration file are as follows:/tmp/script/myscript.conf
Username=tom
filename=/etc/passwd
#!/bin/bash
#configFile =/tmp/script/myscript.conf
[-r/tmp/script/myscript.conf] &&/tmp/script/myscript.conf
Username=$[username:-testuser]
Echo $userName
Example 3: Download a file in a script using the local file download tool wget
#!/bin/bash
Url= ' Http://172.16.0.1/centos6.5repo '
which wget &>/dev/null | | Exit 5
Downloader= ' which wget '
[-X $downloader] | | Exit 6
$downloader $url
Example 4: Write a script to complete the following tasks
1. Assist/var/logs files to the/tmp/logs directory, respectively.
2. Use cp-r when copying a directory
3. When copying files, use CP
4. Copy the linked file cp-d
5. The remaining use of cp-a
#!/bin/bash
#
Targetdir= '/tmp/logs '
[-e $targetDir] | | mkdir $targetDir
For FileName in/var/log/*; Do
If [-D $fileName]; Then
Copycommand= ' Cp-r '
Elif [-F $flieName]; Then
Copycommand= ' CP '
elif [-H $fileName];then
Copycommand= ' cp-d '
Else
Copycommand= ' Cp-a '
Fi
$copyCommand $fileName $targetDir
Done
Bash point of Knowledge: Position parameter rotation (SHIFT)
Write a script that requires the script to be accompanied by parameters, the script function for the parameters and.
#!/bin/bash
#
Declare-i sum=0
For i in ' seq 1 $# '; Do
Let sum+= $i
Shift
Done
Echo $sum
Write a script that uses the following form
Userinfo.sh-u Username [-v {1|2}]
The-u option is used to specify the user, and the script displays the user's UID and GID
If you use the-V option at the same time
The value after-V is 1, and the user's home directory path is displayed extra
The value after-V is 2, and the user's home directory path and Shell are displayed extra
#!/bin/bash
#
[$#-lt 2] && echo "Too less argements, quit" && exit 3
if [["$" = = "-U"]];then
Username= "$"
Shift 2
Fi
If [$#-ge 2] && ["$" = = "-V"];then
Verflag=$2
Fi
If [-N $verFlag];then
if! [[$verFlag =~ [012]]];then
echo "Wrong parameter"
echo "Usage: ' basename '-u username-v {1|2}"
Exit 4
Fi
Fi
#echo $userName $verFlag
If [$verFlag-eq 1];then
grep "^ $userName"/etc/passwd | Cut-d:-f1,3,4,6
elif [$verFlag-eq 2];then
grep "^ $userName"/etc/passwd | Cut-d:-f1,3,4,6,7
Else
grep "^ $userName"/etc/passwd | Cut-d:-f1,3,4
Fi
21. Self-Learning Linux path: File test, parameter rotation