我使用 Notepad++ 在 Windows 中编写了一个 Bash 脚本。
cxStopAllServicesOnSERVER.sh
#!/usr/bin/bash
cd "/some/path"
echo "Hi. Logs can be found at "`pwd`"/cxStartStopLogger.log"
echo "["`date`"]*** STOPPING ALL SERVICES ON SERVER ***" >> "cxStartStopLogger.log"
exit
Run Code Online (Sandbox Code Playgroud)
现在上传并设置所需的文件权限后,我尝试按如下方式执行它:
bash-3.00$ cat cxStopAllServicesOnSERVER.sh #Let's have a look at the code.
#!/usr/bin/bash
cd "/some/path/"
echo "Hi. Logs can be found at "`pwd`"/cxStartStopLogger.log"
echo "["`date`"]*** STOPPING ALL SERVICES ON SERVER ***" >> "cxStartStopLogger.log"
bash-3.00$ # Code and hashbang 'looks' correct,
bash-3.00$ # if there is any issue with the format (EOL …Run Code Online (Sandbox Code Playgroud) 考虑搜索包含模式“搜索字符串”的所有文件的代码:
bash-3.2$ # The below find works fine..
bash-3.2$ find . -type f -exec grep -il "search string" {} \;
bash-3.2$ # But I am unable to redirect output to a log file..
bash-3.2$ find . -type f -exec grep -il "search string" {} \ > log.txt
find: incomplete statement
bash-3.2$
Run Code Online (Sandbox Code Playgroud)
从 Solaris 的手册页中找到:
Run Code Online (Sandbox Code Playgroud)-exec command True if the executed command returns a zero value as exit status. The end of command must be punctuated by an escaped …
考虑源代码:
1. 父.sh
#!/usr/bin/ksh
# No tee
ksh Child.sh;
exit_status=$?;
echo "Exit status: ${exit_status}"
# Using tee
ksh Child.sh | tee -a log.txt;
exit_status=$?;
echo "Exit status: ${exit_status}"
Run Code Online (Sandbox Code Playgroud)
2. 子.sh
#!/usr/bin/ksh
...
exit 1;
Run Code Online (Sandbox Code Playgroud)
输出:
Exit status: 1
Exit status: 0
Run Code Online (Sandbox Code Playgroud)
$exit_status正在捕获 Child.sh 的退出状态,1.$exit_status正在捕获 tee 的退出状态,即0.那么如何捕获退出状态并使用 tee 呢?
1. 考虑片段#1:
$ cat test.txt > test.txt
cat: test.txt: input file is output file
Run Code Online (Sandbox Code Playgroud)
似乎cat使它的输入文件描述符指向 test.txt,然后当它尝试将其输出文件描述符设置为 test.txt 时,它会引发上述错误。这里似乎cat知道重定向运算符,因此继续尝试将输出文件描述符设置为 test.txt
2. 考虑片段#2:
$ cat 1.txt
1:CAT
2:dog
$ sed 's/cat/CAT/g' test.txt
1:CAT
2:dog
$ sed 's/cat/CAT/g' test.txt > test.txt
$ cat test.txt # Note that test.txt is now empty
$
Run Code Online (Sandbox Code Playgroud)
在这里我们看到sed以读取模式打开 test.txt(最后一个参数)并同时设置test.txt为其文件输出描述符。此外,'>'操作员会在sed开始读取文件之前覆盖文件的内容。
我知道管道中的命令并行执行,但没有遇到有关重定向运算符行为方式的任何信息。任何支持链接都会有所帮助。
请考虑:
$ id # Me
uid=100(user1) gid=200(group1) groups=200(group1)
$ ls -l tnsnames.ora # So user1 has only read permission on below file.
-rw-rw-r-- 1 oracle dba 411024 Jul 28 2010 tnsnames.ora
$ cp tnsnames.ora tnsnames.ora_bak
$ cat tnsnames.ora_bak > tnsnames.ora # No write access
-bash: tnsnames.ora: Permission denied
$ cp -p tnsnames.ora tnsnames.ora_copy # Copy file and preserve mode, ownership, timestamps. Requires sudo to succeed completely.
$ ls -l tnsnames.ora_copy # Mode and timestamps preserved; ownership not preserved.
-rw-rw-r-- 1 …Run Code Online (Sandbox Code Playgroud) 我正在尝试递归复制超过 25G 的可安装目录的巨大目录,并希望保留所有者和组 ID、权限模式等。
所以我运行了以下命令:
bash-3.2$ date; cp -rpP /source/path/Oracle /target/path/Oracle;date;
Tue Jun 4 09:44:54 EDT 2013
...
Run Code Online (Sandbox Code Playgroud)
一些观察:
今天(2013 年 6 月 5 日星期三 12:09:25 EDT)我注意到它仍然没有完成并且似乎卡在某个地方。我开始分析这个过程,它似乎在睡觉:
bash-3.2$ truss -p 09431;
read(10, 0xFFBEF288, 32768) (sleeping...)
Run Code Online (Sandbox Code Playgroud)最后一个条目还表明它睡着了。
bash-3.2$ pflags 09431
9431: cp -rpP /source/path/Oracle /target/path/Oracle
data model = _ILP32 flags = RLC|ASYNC|MSACCT|MSFORK
flttrace = 0xfffffbff
sigtrace = 0xfffffeff 0xffffffff
HUP|INT|QUIT|ILL|TRAP|ABRT|EMT|FPE|BUS|SEGV|SYS|PIPE|ALRM|TERM|USR1|USR2|CLD|PWR|WINCH|URG|POLL|STOP|TSTP|CONT|TTIN|TTOU|VTALRM|PROF|XCPU|XFSZ|WAITING|LWP|FREEZE|THAW|CANCEL|LOST|XRES|JVM1|JVM2|RTMIN|RTMIN+1|RTMIN+2|RTMIN+3|RTMAX-3|RTMAX-2|RTMAX-1|RTMAX
entryset = 0x00000401 0x04000000 0x00000000 0x00000028
0x80000000 0x00000000 0x00000000 0x00000000
exitset = 0xfffffffe 0xffffffff 0xffffffff 0xffffffd7
0x7fffffff 0xffffffff 0xffffffff 0xffffffff
/1: …Run Code Online (Sandbox Code Playgroud)我需要创建 UNIX 组的层次结构。像下面这样:
A
|\
| \
B c
|\
D e
|\
f g
Run Code Online (Sandbox Code Playgroud)
...其中 A、B 和 D 是 UNIX 组,而 c、e、f 和 g 是作为这些特定组成员的 UNIX 帐户。我用谷歌搜索了很多,但似乎这是不可能的。
目前,我们有以下几点:
更新:
@John 的帖子让我意识到我需要重新构建我的需求以消除歧义。
我需要的是:
但组 B 的成员需要与组 A 的成员具有相同的权限。(因此,如果组 A 是目录组所有者,则组 B 自动成为目录组所有者)。
顺便说一下,这是一个现实世界的问题,我可以完全控制 B 组及其成员;对其他团体及其成员的控制有限或无法控制。因此,我无法创建新组并向 A 组或 D 组中的成员授予成员资格。
为什么 umask 为我创建的目录和文件设置不同的权限 考虑:
[user@server1 ~]$ umask
0770
[user@server1 ~]$ mkdir TEST2; touch TEST2.txt;
[user@server1 ~]$ ls -l
d------rwx 2 user group_name 4096 Mar 5 05:16 TEST2
-------rw- 1 user group_name 0 Mar 5 05:16 TEST2.txt
Run Code Online (Sandbox Code Playgroud)
现在不应该的文件TEST2.txt具有权限007的umask设置为0770?
我用于rsync创建备份副本:
bash-3.2$ rsync -tvrog /export/home/kent/Scripts /export/home/kent/TEST_Scripts_rsync
Run Code Online (Sandbox Code Playgroud)
使用这些选项:
Options:
-v, --verbose increase verbosity
-r, --recursive recurse into directories
-o, --owner preserve owner (super-user only)
-g, --group preserve group
Run Code Online (Sandbox Code Playgroud)
观察源目录内容:
bash-3.2$ cd /export/home/kent/Scripts
bash-3.2$ ls -l
total 170
-rw-r--r-- 1 kent kent 74 Feb 5 07:10 1.sh
drwxr-xr-x 2 kent kent 2 Jun 17 06:21 456
-rwxrwxrwx 1 kent kent 355 Feb 5 07:10 App_recreate.msh
...
Run Code Online (Sandbox Code Playgroud)
目的地:
bash-3.2$ cd TEST_Scripts_rsync/Scripts/
bash-3.2$ ls -l
total 168
-rw-r--r-- 1 kent kent 74 …Run Code Online (Sandbox Code Playgroud) 我正在努力学习tcpdump。我运行了以下命令:-
tcpdump -i eth0 -lnXs1600 host google.com and port 80
Run Code Online (Sandbox Code Playgroud)
并得到以下结果:-
2:23:45.781779 IP 10.16.30.28.42957 > 173.194.36.65.http: Flags [S], seq 3301439566, win 5840, options [mss 1460,sackOK,TS val 212804497 ecr 0,nop,wscale 5], length 0
0x0000: 4500 003c dbdf 4000 4006 64ad 0a10 1e1c E..<..@.@.d.....
0x0010: adc2 2441 a7cd 0050 c4c7 f84e 0000 0000 ..$A...P...N....
0x0020: a002 16d0 a18e 0000 0204 05b4 0402 080a ................
0x0030: 0caf 2391 0000 0000 0103 0305 ..#.........
12:23:45.782354 IP 173.194.36.65.http > 10.16.30.28.42957: Flags [S.], seq …Run Code Online (Sandbox Code Playgroud)