PATH=roottestexport PATHecho -e Please input a filenameI will check the filenames typeread -p Input a filename filename#test -z $filename && echo you must input a filename && exit 0iftest ! -e $filena
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/testexport 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/eYxF 著作权归作者所有。请勿转载和采集!