我想製作一個像makefile一樣工作的bash腳本。 它會有像-archive,-clean,-backup等選項。 唯一的要求是它必須具有-o參數,因此它指定了一個名稱。 我現在的問題是,我不知道如何從參數中提取.c文件。類似makefile的Bash腳本
例如,如果我inputed ./compile.sh -o名稱-backup hello_world.c print.c
我將如何編譯呢?
下面是我到目前爲止的代碼。
#!/usr/local/bin/bash
if [ $1 != '-o' ]; then
echo "ERROR -o wasn't present as first argument"
echo "HELP"
echo "BASH syntax: $ compile –o filename –clean –backup –archive -help cfilenames"
echo "WHERE:"
echo "$ Unix Prompt"
echo "comiple Name of bash program"
echo "-o filename Mandatory Argument"
echo "-clean Optional and when present deletes all .o files"
echo "-backup Optional and copies all .c and .h files into backup directory"
echo "-archive Optional and Tars content of source directory. Then moved to backup directory"
echo "-help Provides list of commands"
echo "cfilenames Name of files to be compiled together"
fi
NAME=$2
shift
shift
[email protected]
arguments=($options)
index=0
for argument in $options
do
index=`expr $index + 1`
case $argument in
-clean) echo "clean" ;;
-backup) echo "backup"
mv -f *.c ~/backup
mv -f *.c ~/backup ;;
-archive) echo "archive"
tar -zcvf backup.tar.gz *
mv -f backup.tar.gz ~/backup/backup.tar.gz
;;
-help) echo "help"
echo "HELP"
echo "BASH syntax: $ compile –o filename –clean –backup –archive -help cfilenames"
echo "WHERE:"
echo "$ Unix Prompt"
echo "comiple Name of bash program"
echo "-o filename Mandatory Argument"
echo "-clean Optional and when present deletes all .o files"
echo "-backup Optional and copies all .c and .h files into backup directory"
echo "-archive Optional and Tars content of source directory. Then moved to backup directory"
echo "-help Provides list of commands"
echo "cfilenames Name of files to be compiled together"
;;
esac
done
exit;
感謝
爲什麼不直接使用make? – 2012-03-25 21:41:28
我想嘗試新的東西。 – 2012-03-25 21:46:33