我必须编写此脚本来分隔在命令行中输入的参数。 到目前为止这是脚本:

# template.sh
function usage
{
  echo "usage: $0 arguments ..."
  if [ $# -eq 1 ]
  then echo "ERROR: $1"
  fi
}

# Script starts after this line.

case $1 in
  *?)echo "My Name"
     date
     echo
     echo $*
     echo "*****"
     ;;
  *)usage
     ;;
esac

我需要得到的是当我使用它需要的脚本时:

$ ./template.sh peter frederick chang
My Name
Sun May 17 11:28:46 PDT 2015

peter
*****
frederick
*****
chang
*****

但相反,我得到:

$ ./template.sh peter frederick chang
My Name
Sun May 17 11:28:46 PDT 2015

peter frederick chang
*****

===============>>#1 票数:3 已采纳

使用printf代替echo这样的"$@"来打印不同行的参数:

# Script starts after this line.
case $1 in
  *?)echo "My Name"
     date
     echo
     printf "%s\n*****\n" "$@"
     ;;
  *)usage
     ;;
esac

这将打印:

My Name
Sun May 17 06:50:26 EDT 2015

peter
*****
frederick
*****
chang
*****

  ask by PFKrang translate from so

未解决问题?本站智能推荐: