请问一下bash命令行参数

sh/bash/dash/ksh/zsh等Shell脚本
回复
头像
ChenFengyuan
帖子: 770
注册时间: 2008-03-23 0:39

Re: 请问一下bash命令行参数

#1

帖子 ChenFengyuan » 2010-09-05 16:50

代码: 全选

$ cat test.sh 
a=$(cat);echo $a
$echo "hello world"|bash test.sh
hello world
头像
ChenFengyuan
帖子: 770
注册时间: 2008-03-23 0:39

Re: 请问一下bash命令行参数

#2

帖子 ChenFengyuan » 2010-09-05 16:53

read a;echo $a
还是用read比较smart
太复杂的可以上perl,lol
头像
ChenFengyuan
帖子: 770
注册时间: 2008-03-23 0:39

Re: 请问一下bash命令行参数

#3

帖子 ChenFengyuan » 2010-09-05 16:55

pocoyo 写了:谢了 原来是凤媛啊 :em04 cfy :em04
:em06
头像
ChenFengyuan
帖子: 770
注册时间: 2008-03-23 0:39

Re: 请问一下bash命令行参数

#4

帖子 ChenFengyuan » 2010-09-05 17:03

我觉的是把数据放在stdin里了么,然后用个方法获取stdin即可,所以用$(cat)或者read foo :em11
头像
ChenFengyuan
帖子: 770
注册时间: 2008-03-23 0:39

Re: 请问一下bash命令行参数

#5

帖子 ChenFengyuan » 2010-09-05 18:46

不从stdin,从参数里?写个if判断下?
我来学习的
帖子: 3
注册时间: 2010-09-05 18:29

Re: 请问一下bash命令行参数

#6

帖子 我来学习的 » 2010-09-05 19:41

$ echo aa | sh test.sh -n
aa
$ cat test.sh
#!/bin/sh
while [ -n "$1" ]; do
case $1 in
-n)opt_r="yes";shift 1;;
--)shift;break;;
*)break;;
esac
done

if [ "$opt_r" = "yes" ] ; then read a ;else a="1" ;fi
echo $a
[/code]
LZ新手写的有写长不知可不可以 :em06
回复