Dipankar Deka Dipankar Deka - 1 month ago 6
Bash Question

How to write file creation script?

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.

Answer

Using vi as the editor:

D=$2
if [ -z "$D" ] ; then
    D="."
fi

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

vi "$D/$F"