Bash Script: File Type Checker and Manipulation
This is a Bash script that prompts the user to input a filename and then checks the type of the file. If the file does not exist, it creates it. If it is a directory, it deletes it and creates a new one. If it is a regular file, it deletes it. If it is neither a directory nor a file, it exits the script.
Here is a breakdown of what each line does:
PATH=/root/test: sets the PATH environment variable to '/root/test'export PATH: exports the PATH environment variable so that it is available to child processesecho -e 'Please input a filename,I will check the filename's type': prints a message asking the user to input a filenameread -p 'Input a filename:' filename: prompts the user to input a filename and stores it in the variable filenameif[test ! -e '$filename' ]; then: checks if the file does not existtouch '$filename': creates the file if it does not existelif[test -d '$filename' ]; then: checks if the file is a directoryrm -rf '$filename': deletes the directory if it existsmkdir '$filename': creates a new directory with the same nameelif[test -f '$filename']; then: checks if the file is a regular filerm -rf '$filename': deletes the file if it existselse: if the file is neither a directory nor a fileexit 0: exits the script with a status of 0 (success)
原文地址: https://www.cveoy.top/t/topic/n0td 著作权归作者所有。请勿转载和采集!