Write a second shell script today and write the after-class questions in "private cuisine". The problem needs to write a check whether a name exists, does not exist to create a file, there is a need to determine whether the name of the file, the file is deleted after the creation of a directory with the same name, if the directory is to delete this directory.
First set up a basic condition: The Operation directory is/tmp/test/(note: If this directory does not exist you need to create manually), you need to establish a file or directory name of logical.
Start writing scripts below.
#!/bin/bash#program:# this program to check if a filename exists  #History #2016/04/08 mg First releasePATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/sbin:~/binexport pathecho -e "Checking if/tmp/test/logical is present, please later ..." test -e "/tmp/test/logical" && state=0 | | state=1path= "/tmp/test" filename= "logical" case $state in "1") echo "The file does not exist, is creating ..." mkdir -p $path && touch "$path/$filename" echo "File created successfully! " ;; *) test -d "$path /$filename " && STATU=0&NBsp;| | statu=1 if [ "$statu" == "0" ];then echo $filename exists and is a directory. " rm -rf "$path/$filename" echo "deleted this directory! " else echo "$filename exists and is a file, deleting and creating a directory with the same name " rm -f "$path/$filename" && mkdir "$path/$filename" echo "Creating now Please ..." echo "$filename Catalogue has been established! " fi ;; esac
For simple instructions, write the logic of the script. Use the TEST-E command to determine if the "logical" name exists, if no status is set to "1" and create a file based on the topic. If there is a test-d command to determine whether the directory, if the directory is set its state to 0, and the directory through the RM-RF command to delete (note: If you do not add the-R parameter can not delete the directory, so you need to join the-R parameter), if the file is the rm-f command to delete the file, Create a directory with the same name by using the mkdir command (Note: "Command 1 && command 2" means that if command 1 executes successfully, the command 2 continues to execute).
The script function is relatively simple, there will be some shortcomings, continue to work to improve it.
This article from "Product small stream, into Jianghai" blog, please be sure to keep this source http://goodjoe.blog.51cto.com/9173556/1761796
Bash Shell Scripting Learning (ii)