日期:2014-05-16  浏览次数:20704 次

问问 关于Shell的重定向~~~
如果自己写个脚本,在调用的时候是可以指定重定向的,
比如 ./install.sh > install.log
但是这有个缺点,比较麻烦,
而且不能保证别人调用的时候都这么写,
我现在想:不管是谁调用,都留个日志,
所以,能不能在 install.sh 脚本里,自己给自己重定向呢?

谢谢~

------解决方案--------------------
可以这样试试:

Assembly code

[root@RHEL6A shcode]# ls
config           nohup.out  sh12.sh  sh14.sh  sh16.sh  sh18.sh  sh20.sh  sh22.sh  sh24.sh  sleep.sh
ftpautologin.sh  sh11.sh    sh13.sh  sh15.sh  sh17.sh  sh19.sh  sh21.sh  sh23.sh  sh25.sh  temp.sh
[root@RHEL6A shcode]# ./sh25.sh 
[root@RHEL6A shcode]# ls
config           nohup.out  sh13.sh  sh16.sh  sh19.sh  sh22.sh  sh25.sh
ftpautologin.sh  sh11.sh    sh14.sh  sh17.sh  sh20.sh  sh23.sh  sleep.sh
mylog.txt        sh12.sh    sh15.sh  sh18.sh  sh21.sh  sh24.sh  temp.sh
[root@RHEL6A shcode]# cat mylog.txt 
hello
ls: cannot access aaabbbccc: No such file or directory
[root@RHEL6A shcode]# cat sh25.sh 
#!/bin/sh
exec 1>mylog.txt 2>&1
echo 'hello'
ls aaabbbccc

[root@RHEL6A shcode]#

------解决方案--------------------
exec就可以了。
------解决方案--------------------
exec 1>mylog.txt 2>&1
基础
------解决方案--------------------
这种问题自己试一下不就知道了,还是用前面贴的代码示例:

Assembly code

[root@RHEL6A shcode]# ./sh25.sh >> mylog2.txt
[root@RHEL6A shcode]# ls
config           mylog.txt  sh12.sh  sh15.sh  sh18.sh  sh21.sh  sh24.sh   temp.sh
ftpautologin.sh  nohup.out  sh13.sh  sh16.sh  sh19.sh  sh22.sh  sh25.sh
mylog2.txt       sh11.sh    sh14.sh  sh17.sh  sh20.sh  sh23.sh  sleep.sh
[root@RHEL6A shcode]# cat mylog.txt 
hello
ls: cannot access aaabbbccc: No such file or directory
[root@RHEL6A shcode]# cat mylog2.txt 
[root@RHEL6A shcode]#