The bash knowledge point for the shell exercises (for loop, if sentence structure exercises)

Source: Internet
Author: User

Bash Knowledge Points: File testing
[ ]
[[ ]]
Test
Monocular test:
-e file: Test files exist
-A file: Test files exist
-F file: Test for normal files
-D directory: test for directory files
-H file: Test is a linked file
-R somefile: Test whether its valid users have read access to this file
-W somefile: Test Whether its valid users have write access to this file
-X Somefile: Tests whether its valid users have Execute permissions on this file

-b somefile : 测试文件是否存在并且是否为一个块设备文件-c somefile : 测试文件是否存在并且是否为一个字符设备文件-h|-L somefile : 测试文件是否存在并且是否为符号链接文件-p somefile : 测试文件是否存在并且是否为管道文件:-S somefile : 测试文件是否存在并且是否为套接字文件:-s somefile:  测试文件是否存在并且不空

Binocular test:
File1-nt file2: Test if file1 is newer than file2
File1-ot file2: Test if file1 is older than file2

Pour other files into the script using the source command
[-R Profile] && cource configuration file

Bash's knowledge point: Position parameter rotation
Shift

Exercises:
First, edit a script to determine if the/tmp directory has no 1 This directory, if there is to exit, if not, create the directory 1
1) Execute the following command at the command line:
Filename=/tmp/1
[-e $fileName] | | mkdir $fileName

Explanation: 1. Using variable FILENAME=/TMP/1
2.[] in brackets-E means: true (True) if directory 1 is in directory/tmp; False (FALSE)
3. True (TRUE) does not execute subsequent Mkdie $fileName, False (false) executes

Second, write a script that can invoke the. conf configuration file
2.1 First create a. conf configuration file in the/tmp/scripts directory
[Email protected] scripts]# vim myscripts.conf

Username=tom
filename=/etc/passwd

2.2 Start writing myscripts.sh scripts
[Email protected] scripts]# vim myscripts.sh
#!/bin/bash
Source/tmp/scripts/myscripts.conf
Echo $userName

**2.3 to myscripts.sh script definition * * *
#!/bin/bash
#configfile:/temp/scripts/myscripts.conf
[-r/tmp/scripts/myscripts.conf] && source/tmp/scripts/myscripts.conf #source可以用.
Username=${username=jerry}
Echo $userName

Explanation: 1. Create a myscripts.conf configuration file
2. Use Vim to edit the myscripts.sh script
3. Add comments, configure file directory path,
4. Use [-R file Absolute path] to determine whether the file exists, #-r indicates whether the current user has read access to the file
5. If the preceding is true (true) then use source or. Call the myscripts.conf configuration file and export the contents of the username variable in the myscripts.conf
6. If the front is False (false), then ignore; directly print the contents of variables defined in the script (output: Jerry)

C. Write a script to copy the/var/log to the/tmp/logs
We can do a little test before we write the script:
[email protected] scripts]# which wget
/usr/bin/wget
[[email protected] scripts]# echo $?
0

Explanation: Using wich wget to determine the wget This command does not exist

Create a script vim download.sh finished writing using the-n check under script syntax (Sh-n download.sh)
[Email protected] scripts]# vim download.sh

#!/bin/bash
Url= ' Ftp://192.168.100.157/hzftp/IP%C9%A8%C3%E8%B9%A4%BE%DF.exe '
which wget &>/dev/null | | Exit 5
Downloader=which wget
[-x$downloader] | | Exit 6
$downloader $url

Explanation: 1. Add a URL
2. Use which to determine that the wget does not exist, output the result to null (>/DEV/NULL), or exit if it does not exist.
3. Use variable downloader to define which wget
4. Then use [-X] to determine whether the current user has execute permissions on the downloader, and no Exit
5. Perform $downloader $url (effect equals wget ftp://192.168.100.157/hzftp/IP%C9%A8%C3%E8%B9%A4%BE%DF.exe)

Four, write a script to complete the following tasks:
1. Copy the files under/var/log separately to the/tmp/logs directory
2. Use cp-r when copying a directory
3. When copying files, use CP
4. Copy the linked file, using cp-d
5. If it is a different file, use Cp-a

[Email protected] scripts]# vim CPLOG.SHS

 copyCommand=‘cp‘

Elif [-F $fileName]; Then
#!/bin/bash
#
Targetdir= '/tmp/logs '

[-e $targeDir] | | mkdir $targeDir
For FileName in/var/log/*;d o
If [-D $fileName]; Then
Copycommand= ' Cp-r '
Elif [-F $fileName]; Then
Copycommand= ' CP '
elif [-H $fileName]; Then
Copycommand= ' cp-d '
Else
Copycommand= ' Cp-a '
Fi

$copyCommand $fileName $targetDir
Done

Explanation: 1. Edit the variable target directory (TargetDir)
2. Determine if the target directory exists, does not exist, and creates
3.for Loop Statement (copies all files below the/var/log)
4.if Judgment Statement ([-d] corresponding to the directory file, [-f] corresponding to the normal file, [-h] corresponding to the linked file, [-a] corresponding to other files
5. Sequence the variables sequentially (copy the command file target directory)
~
~
~
~
~
~
Five, write a script to complete the following tasks
script.sh {Start|stip|restart|status}
If blank, displays help information script.sh {start|stip|restart|status}

The bash knowledge point for the shell exercises (for loop, if sentence structure exercises)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.