mao2047
mao2047

Reputation: 101

Bash script that creates a file with a introduced name

I was wondering how to create a bash script that asks the user for the desired name, and then, creates a file with that name.

something like:

introduce name: test [enter]
(then use that name as the name of the final zip)
zip -9 test.zip *
(creating the file)
"test.zip" crated.
(print out that message)
end

Upvotes: 0

Views: 95

Answers (2)

Gilles Quénot
Gilles Quénot

Reputation: 185171

Try this:

read -p 'introduce name:' input
zip -9 $input.zip *

Check

help -m read | less

If you are a beginniner, some good pointers to start learning :

FAQ: http://mywiki.wooledge.org/BashFAQ
Guide: http://mywiki.wooledge.org/BashGuide
Ref: http://www.gnu.org/software/bash/manual/bash.html
http://wiki.bash-hackers.org/
http://mywiki.wooledge.org/Quotes
Check your script: http://www.shellcheck.net/

And avoid recommendations of people saying to learn with tldp.org web site, the tldp bash guide is outdated, and in some cases just plain wrong.

Upvotes: 3

mao2047
mao2047

Reputation: 101

I wrote something like this

read -p 'introduce name:' input
ext=$(sed -n 's/\(^.[^$]*\)\(.\{3\}$\)/\2/p' <<< $input)
if [ $ext = zip ]
    then zip -9 $input *
elif [ $ext = cbz ]
    then zip -9 $input *
elif [ $ext = rar ]
    then rar a $input *
else 
    echo "I just zip, cbz and rar, sorry :("
fi

Upvotes: -1

Related Questions