photon
9/24/2018 - 3:06 AM

touch sh bash files with additional operations such as give file names and chmod them

touch sh bash files with additional operations such as give file names and chmod them

#!/bin/sh

# Once the global warmth reaches a tipping point,
#     the chain-reaction could be irreversible,
#     so are we ready for the ticking bomb?

# Usage:
# ./touchsh file_name
# No touch operation if file already existed.

n_touch(){
    fpath=$1

    printf "$fpath" | grep -Eq '\.bash$'

    if [ 0 -eq $? ] 
        then
        echo "NOTICE:: Found name suffix .(ba)sh in input"
        fpath_new=$fpath
    else
        fpath_new=$fpath.bash
    fi

    if [ -e $fpath_new ]
        then
        echo "NOTICE:: $fpath_new"
        echo "NOTICE:: File already existed. Exit with no operation."
        return 1
    fi
    
    touch $fpath_new
    printf "#!/bin/bash\n\n" > $fpath_new
    chmod u+x $fpath_new
    echo $fpath_new
}

fpaths_str="$@"
for fpath in $fpaths_str
    do
    n_touch $fpath
done
#!/bin/sh

# Usage:
# ./touchsh file_name
# No touch operation if file already existed.

n_touch(){
    fpath=$1

    printf "$fpath" | grep -Eq '\.sh$'

    if [ 0 -eq $? ] 
        then
        echo "NOTICE:: Found name suffix .(ba)sh in input"
        fpath_new=$fpath
    else
        fpath_new=$fpath.sh
    fi

    if [ -e $fpath_new ]
        then
        echo "NOTICE:: $fpath_new"
        echo "NOTICE:: File already existed. Exit with no operation."
        return 1
    fi

    touch $fpath_new
    printf "#!/bin/sh\n\n" > $fpath_new
    chmod u+x $fpath_new
    echo $fpath_new
}

fpaths_str="$@"
for fpath in $fpaths_str
    do
    n_touch $fpath
done