1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | #!/bin/bash # Shell script to create files and directories that do not exist # This script also demonstrate use of functions and command line arguments using getopts command # ------------------------------------------------------------------------- # Copyright (c) 2004 nixCraft project <http://www.cyberciti.biz/fb/> # This script is licensed under GNU GPL version 2.0 or above # ------------------------------------------------------------------------- # This script is part of nixCraft shell script collection (NSSC) # Visit http://bash.cyberciti.biz/ for more information. # ---------------------------------------------------------------------- usage(){ echo "Usage: $0 {-f filename} {-d dirname}" exit 1 } createDir(){ if [ ! -d $1 ] then /bin/mkdir -p $1 >/dev/null 2>&1 && echo "Directory $1 created." || echo "Error: Failed to create $1 directory." else echo "Error: $1 directory exits!" fi } createFile(){ if [ ! -f $1 ] then touch $1 > /dev/null 2>&1 && echo "File $1 created." || echo "Error: Failed to create $1 files." else echo "Error: $1 file exists!" fi } while getopts f:d:v option do case "${option}" in f) createFile ${OPTARG};; d) createDir ${OPTARG};; \?) usage exit 1;; esac done |
Yeah, this script isn’t multi-process safe. Is there any other way, which is multiprocess safe as well?
This script isn’t multi-process safe.
Test it by calling the same process with same file names multiple times in a wrapper script. ‘touch’ only changes last modification time, couldn’t tell you if a file is already been created.
michael: u can run this script by below.
shell> chmod +x script_name
shell> ./script_name
above is one way of running the script where script_name is the name you give to the shellscript you saved.
you can also run it as below;
shell> sh script_name
How to run this script??