# define some variables for later
date=`date`
usr=`whoami`
# define usage function to echo syntax if no args given
usage(){
echo "error: filename not specified"
echo "Usage: $0 filename directory/ directory/ directory/"
exit 1
}
# define copyall function
copyall() {
# local variable to take the first argument as file
local file="$1" dir
# shift to the next argument(s)
shift
# loop through the next argument(s) and copy $file to them
for dir in "$@"; do
cp -R "$file" "$dir"
done
}
# function to check if filename exists
# $f -> store argument passed to the script
file_exists(){
local f="$1"
[[ -f "$f" ]] && return 0 || return 1
}
# call usage() function to print out syntax
[[ $# -eq 0 ]] && usage
这是我无法弄清楚的
# call file_exists() and copyall() to do the dirty work
if ( file_exists "$1" ) then
copyall
我还想知道如何采用下一个回声部分并将其压缩到一行。而不是1美元然后转移然后继续前进。也许把它分成一个数组?
echo "copyall: File $1 was copied to"
shift
echo "$@ on $date by $usr"
else
echo "Filename not found"
fi
exit 0
答案 0 :(得分:1)
在我看来,file_exists
宏是多余的:
if [ -f "$1" ]
then copy_all "$@"
else echo "$0: no such file as $1" >&2; exit 1
fi
甚至只是:
[ -f "$1" ] && copy_all "$@"
答案 1 :(得分:0)
您可能只需要删除file_exists "$1"
周围的括号,然后添加分号:
if file_exists "$1"; then
copyall