需求:
在某些软件安装过程中,要创建独立用户,然后以该用户运行某些安装操作,如gitolite安装过程,创建git用户并执行安装
方法1:sudo -u user ...
sudo -u git -H <<-Code
ls ~
cat test
#other cmds
Code
方法2: su
下面有些摘录
==su命令
#!/bin/sh
echo ~/
su - test <<HERE
echo ~/
ls -al
HERE
echo ~/
sudo su - user1
echo #HOME
echo `ls -ltr`
可以用sudo命令,它的作用是“excute a command as another user",默认为root用户,可以用 -u 指定用户名(假设要以user身份执行prog程序):sudo -u user prog
就可以了。
另一个命令su,是用另一个身份运行shell,再用新的shell运行指定程序(su只能运行shell程序,虽然可以把命令写成类似sudo的样子[su -c user prog],但它的实现原理实际上还是先运行一个shell,再通过shell运行其他程序)。
http://www.shareup.me/metadocs/11197
Shell中通常将EOF与 << 结合使用,表示后续的输入作为子命令或子Shell的输入,直到遇到EOF为止,再返回到主调Shell。如下:
export NLS_LANG='SIMPLIFIED CHINESE_CHINA.ZHS16GBK'
sqlplus $USERID @$OUT_FILE << EOF
exit
EOF
当把这段代码放在Shell中时,会在Shell脚本执行完毕后自动退出SQLPLUS的命令提示符状态。
<<EOF(内容)
EOF
可以把EOF替换成其他东西
意思是把内容当作标准输入传给程序这里再简要回顾一下< <的用法。当s h e l l看到< <的时候,它就会知道下一个词是一个分界
符。在该分界符以后的内容都被当作输入,直到s h e l l又看到该分界符(位于单独的一行)。这个
分界符可以是你所定义的任何字符串。
方法1.需要,自动登录mysql(root:root,passwd:123456),查询test库,test1表里的user=aa的记录.
#!/bin/sh
mysql -uroot -p123456 <<EOF
use test;
select * from testaa while a=10000; ###1000 not usr single quote mark,because a is int
# type,only char type need single quote mark.
exit
EOF
方法2:用expect脚本
3
1 #!/bin/bash
2 #batch_connect.sh
3
4 #cat /dev/null > batch_file
5 echo "use oss_platform;" > batch_file
6
7 URL_list=`cat url_for_db_final`
8 for URL in $URL_list
9 do
10 echo "update pornURL set count=count+1 where url=\"$URL\";" >> batch_file
11 echo "insert into pornURL (url,count) select \"$URL\",1 from dual where not exists (select * from pornURL where
url=\"$URL\");" >> batch_file
12
13 done
14 echo "exit" >> batch_file
15
16
17 cat /dev/null > url_for_db_final
18 mysql -h localhost -u ******* -p*************< batch_file | more
19
20
21 cat /dev/null > batch_file
http://hi.baidu.com/dengmiracle/item/1177448f0d590817c31627bf
a. 切换用户只执行一条命令的可以用: su - oracle -c command
b. 切换用户执行一个shell文件可以用:su - oracle -s /bin/bash shell.sh
filepath=$(cd "$(dirname "$0")"; pwd)
http://www.cnblogs.com/iloveyoucc/archive/2012/05/11/2496433.html
开机自启动可以讲脚本放到/etc/rc.d目录下的rc.local中即可