学习shell编程 – 4. 脚本参数、输入输出



#!/bin/sh

#如果在外面调用 hello.sh a b 
echo $0  #显示 hello.sh
echo $1  #显示 a
echo $2  #显示 b
echo $#  #显示 2,即2个参数

#用while + getopts处理所有选项
while getopts c:p OPTION ; do
  case "$OPTION" in
     c) echo "c is input" ;;
     p) echo "p is input" ;;
     \?) echo "Please specify -c or -p" ;
         exit 1 ;;
  esac
done

################################################################
#输出
echo abc #会自动换行
printf abc\n  #需显式指定换行
#printf可以做格式化
for f in /home/kent/*; do printf "%32s\n" $f;  done;

#读取用户输入
printf "Input your name, buddy:"
read name
echo Your name is $name


Leave a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.