How to write file creation script? [closed]

2019-09-21 02:32发布

问题:

I am very new to scripting and would like to know how to write a script fileCreate which has two optional parameters two input arguments as shown below:

fileCreate <filename> <path>

<filename> parameter signifies the name of the file in which the contents will be saved

<path> parameter signifies the directory structure where the file will be saved.

When the program is executed it will open an editor and user can input any contents, that will be saved as specified.

If no parameters are passed, the default is current directory and userfileX (userfileX to the number of times the file is created)

If there already exists a file of similar name and having same contents then the user should be able to append the file or else just the date gets modified.

回答1:

Using vi as the editor:

D=${2:-.}

F=$1
if [ -z "$F" ] ; then
    X=1
    F="userfile$X"
    while [ -f "$F" ] ; do
        X=$(($X+1))
        F="userfile$X"
    done
fi

vi "$D/$F"


标签: linux shell sh