我想在两个文件中搜索多个字符串。如果在两个文件中都找到一个字符串,则做一些事情。如果只在一个文件中找到一个字符串,那么做另一件事。
我的命令是下一个:
####This is for the affirmative sentence in both files
if grep -qw "$users" "$file1" && grep -qw "$users" "$file2"; then
####This is for the affirmative sentence in only one file, and negative for the other one
if grep -qw "$users" "$file1" ! grep -qw "$users" "$file2"; then
Run Code Online (Sandbox Code Playgroud)
否定和肯定这些陈述的方式是否正确?pd 我正在使用 KSH 外壳。
先感谢您。
我使用 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) 运行 kornshell 并尝试遍历目录树。想cd到一个如下命名的子目录:
-3ab_&_-3dc.img
Run Code Online (Sandbox Code Playgroud)
我的问题是我需要如何转义此名称中的&符号?我尝试了双引号和反斜杠的不同组合,但没有成功。
考虑源代码:
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 呢?
我有一个如下所示的文件
--------------------------------------------------------------
Name_Customer Item_Purchased Item_Amount Credit
--------------------------------------------------------------
Tom H1_P 7657 N/A
Pras Track_1 23 N/A
Cha Brace 9 N/A
Moh kite37 269 N/A
Prab Bols 87699 N/A
Run Code Online (Sandbox Code Playgroud)
我需要Item_Amount通过忽略文件中的标题来添加列下的值并将总和打印为
Total Amount collected = 95657
Run Code Online (Sandbox Code Playgroud) 我想打印文件中的所有行,直到匹配词请建议如何使用 awk
例如
我想打印所有行,直到单词 PPP
备注第一行与 AAA 不同(任何单词)
cat file.txt
AAA ( the first line/word chuld be any word !!!!! )
BBB
JJJ
OOO
345
211
BBB
OOO
OOO
PPP
MMM
(((
&&&
Run Code Online (Sandbox Code Playgroud)
所以我需要得到这个
AAA
BBB
JJJ
OOO
345
211
BBB
OOO
OOO
PPP
Run Code Online (Sandbox Code Playgroud)
其他示例(想要打印到 KJGFGHJ )
cat file.txt1
HG
KJGFGHJ
KKKK
Run Code Online (Sandbox Code Playgroud)
所以我需要得到
HG
KJGFGHJ
Run Code Online (Sandbox Code Playgroud) 是否可以在文件中的特定列上使用差异?
Something 123 item1
Something 456 item2
Something 768 item3
Something 353 item4
Run Code Online (Sandbox Code Playgroud)
Another 123 stuff1
Another 193 stuff2
Another 783 stuff3
Another 353 stuff4
Run Code Online (Sandbox Code Playgroud)
Something 456 item2
Something 768 item3
Another 193 stuff2
Another 783 stuff3
Run Code Online (Sandbox Code Playgroud)
我想要diff每个文件的第二列,然后,结果将包含 diff-ed 列,但与整行一起。
我有一个像下面这样的文件
var 3 2014 string
var1 4 2011 string4
var2 6 1999 string2
var3 1 2016 string6
Run Code Online (Sandbox Code Playgroud)
然后我有这个 while read 循环来将其中一列与一个数字进行比较,然后回显一些东西。然而,它没有回应我想要的短语,而是回应了别的东西。
while read num
do
if [ "$num" = "0" ]; then
echo "Number is equal to zero"
else
echo "number is not equal to 0"
fi
done < home/dir/file.txt | awk '{print $2}'
Run Code Online (Sandbox Code Playgroud)
它不是回应上述内容,而是回应文件的第二列。
我正在尝试执行 telnet 命令并将输出放在 shell 脚本上。但是当端口没有打开或ip不存在时。该命令花费太多时间来给出响应。是否可以限制 telnet 上的最大尝试时间?
telnet host port
Run Code Online (Sandbox Code Playgroud) 我已经在 bash/ksh 中声明了函数和变量,我需要将它们转发到sudo su - {user} << EOF:
#!/bin/bash
log_f() {
echo "LOG line: $@"
}
extVAR="yourName"
sudo su - <user> << EOF
intVAR=$(date)
log_f ${intVAR} ${extVAR}
EOF
Run Code Online (Sandbox Code Playgroud)