Shell Io   

Shell 的输入输出

1. echo命令

#!/bin/bash
echo -e "What is your name:\c"
read name
echo $name

2. cat 命令

$ cat > myfile
This is great
<CTRL+D>
$ pg myfile
This is great

3. tee

who | tee who.out 我们使用 who命令,结果输出到屏幕上,同时保存在文件who.out中

4. 标准输入/输出/错误

5. 读文件 shell 脚本

#! /bin/sh
if [  -f $1  ]; then
cat $1 |
while read cmd
do
	c++filt $cmd
done
fi

6. 带颜色的输出

# alert an error msg
function alert()
{
	echo -en "\\033[40;31m" # set font color as red
	echo -en "$*"
	echo -en "\\033[0;39m" # restore font color as normal
}

function success()
{
	echo -en "\\033[40;32m" # set font color as green
	echo -en "$*"
	echo -en "\\033[0;39m" # restore font color as normal
}

7. 运行命令不输出

# run program and do not display the output
function run_quiet()
{
	local cmd=$*
	$cmd >/dev/null 2>&1
	return $?
}

8. 实现返回指针

[root@hhb-kvm ~]# cat ./tst.sh
#!/bin/sh
function edit()
{
	p=$1
	eval $p=1045
}
x=100
px=x
echo $x
edit $px
echo $x