
Thihup
(usa Manjaro Linux)
Enviado em 20/04/2015 - 13:19h
Amigo, é um pouco complexo com o esquema de "-h" e talz, mas eu posso ajudar :
#!/bin/bash
case $* in
"-h") echo "Isto seria uma ajuda... Mas fiquei com preguiça de escrevê-la."
;;
"-v") echo "Versão 666."
;;
*) echo "Opção inválida!"
exit 1
;;
esac
Ou por exemplo:
#!/bin/bash
function PrintUsage() {
echo "Uso: `basename $0` <-umsf> [-ohv]"
exit 1
}
while getopts "hvo:umsf" OPTION
do
case $OPTION in
h) PrintUsage
;;
v) echo "`basename $0` versao 666."
exit
;;
o) ARQUIVO_LOG=$OPTARG
;;
u) DO_UNAME=1
;;
m) DO_FREE=1
;;
s) DO_SWAPON=1
;;
?) PrintUsage
;;
esac
done
shift $((OPTIND-1))
if [ -z "$DO_UNAME" ] && [ -z "$DO_FREE" ] && [ -z "$DO_SWAPON" ] && [ -z "$DO_FDISK" ]; then
PrintUsage
fi
if [ "$ARQUIVO_LOG" ]; then echo "Execucao iniciada em `date`." >> $ARQUIVO_LOG
if [ "$DO_UNAME" == 1 ]; then
uname -a >> $ARQUIVO_LOG
fi
if [ "$DO_FREE" == 1 ]; then
free -m >> $ARQUIVO_LOG
fi
if [ "$DO_SWAPON" == 1 ]; then
swapon -s >> $ARQUIVO_LOG
fi
else
echo "Execucao iniciada em `date`."
if [ "$DO_UNAME" == 1 ]; then
uname -a
fi
if [ "$DO_FREE" == 1 ]; then
free -m
fi
if [ "$DO_SWAPON" == 1 ]; then
swapon -s
fi
fi
Então adapte o código a sua necessidade.
Espero ter ajudado
Se ajudei, marque a próxima resposta desse tópico (fora a minha) como melhor resposta =D
[]'s
T+
Fonte e Link util :
http://www.devin.com.br/shell-script-tratamento-de-argumentos-e-opcoes/