Uma versão melhor desta função:
mnt() {
typeset dir # for local scope for the variable.
# assumes ksh88/pdksh/bash/zsh/yash. Replace typeset with
# local for ash-based shells (or pdksh/bash/zsh)
dir=$1 # here it's the only place where you don't need the quotes
# though they wouldn't harm
mkdir -p -- ~/tmp/"$dir" || return
# quotes needed. mkdir -p creates intermediary directories as required
# and more importantly here, doesn't fail if the directory already
# existed.
# We return from the function if mkdir failed (with the exit status
# of mkdir). The -- is for the very unlikely even that your $HOME starts
# with "-". So you may say it's a pedantic usage here. But it's good
# habit to use it when passing an arbitrary (not known in advance)
# argument to a command.
sudo mount "/dev/$dir" ~/tmp/"$dir" || return
# Or use /usr/bin/sudo if there are more than one sudo commands in $PATH
# and you want to use the one in /usr/bin over other ones.
# If you wanted to avoid calling an eventual "sudo" function or alias
# defined earlier in the script or in a shell customisation file,
# you'd use "command sudo" instead.
cd -P -- ~/tmp/"$dir" # another pedantic use of -P for the case where
# your $HOME contains symlinks and ".." components
}
Ponto-e-vírgula não é necessário. Enquanto no prompt do seu shell, você escreve:
cd /some/where
ls
Não
cd /some/where;
ls;
Não é diferente em scripts. Você usa ;
para separar comandos em uma linha como em:
cd /some/where; ls
No entanto, você prefere escrever:
cd /some/where && ls
Isso não é executado ls
incondicionalmente, mas somente se cd
for bem-sucedido.