尝试将参数传递给函数时遇到错误。
#! /usr/local/bin/bash
program=$(basename $0)
file_info () {
# file_info: function to display file infomation.
echo "this is $1"
if [[ -e $1 ]]; then
echo "Paramater 1 eq $1"
echo -e "\nFile Type:"
file "$1"
echo -e "\n File Status:"
stat "$1"
else
echo "$program: usage: $program file" >&2
return 1
fi
}
file_info
我测试了
$ bash file_info.sh answer.sh
this is .
file_info.sh: usage: file_info.sh file
然而文件存在。
$ [[ -e answer.sh ]] && echo "answer.sh exists"
answer.sh exists
如何将位置参数传递给函数。
答案 0 :(得分:1)
要将脚本参数传递给函数,请将最后一行更改为:
file_info "$@"