我正在开发这个脚本:
#!/bin/bash
# If no command line arguments, display a tiny usage message.
if [[ $# -eq 0 ]]; then
echo "Usage: $0 files..." >&2
exit 1
fi
# Destroy child processes, when exiting.
cleanup() {
kill `jobs -p` 2>/dev/null
}
# Call cleanup function on SIGINT, ie. Ctr
只想在Bash脚本中的变量中保存kubectl命令的输出。
例如:命令kubectl get all -n mattermost输出No resources found in mattermost namespace.
foo=$(kubectl get all -n mattermost)
while [ "$foo" = "No resources found in mattermost namespace." ]
do
echo "yay"
done
不幸的是,输出不会保存在变量中。
我尝试在脚本中创建一个别名,如下所示:
#!/bin/bash
shopt -s expand_aliases
#Currently just using 'git' to try and get it working
#It'll be replaced with the following line when it works
#compgen -c | while read x;
echo 'git' | while read x;
do
a=`echo $x | tr '[:lower:]' '[:upper
我希望在目录中的所有.fa文件中运行一个程序(锚),并将输出追加到输入文件中(作为原始输入内容的下一行)。为此,我尝试过:
for f in ./*.fa ; do ./anchor $f -d ./; done >> $f
它给出了错误:
bash: $f: ambiguous redirect
我知道bash反对在输入文件中发布输出内容,但是由于我最近从windows系统迁移,所以我这样做:
for %F in ("*.fa") do anchor %F -d ./ >> %%F
这给了我想要的输出。
虽然在输入文件中添加输出似乎很奇怪,但我如何在s
下面的解决方案找到了,我尝试在bash中实现元组上的循环:
NUMBERS='1,2,3 4,5,6 7,8,9'
for TOUPLE in $NUMBERS;
do IFS=',';
set -- $TOUPLE
echo \($1, $2, $3\)
done
echo ''
for TRIPLE in $NUMBERS;
do IFS=',';
set -- $TRIPLE
echo \($1, $2, $3\)
done
这些应该基本上是相同的循环,它们应该打印相同的输出。但是,当我
我试图将bash输出重定向到一个变量文件名。这是我的剧本
#!/bin/bash
for i in `cat servers`
do
if [ "$i" = "198.162.1.3" ];
then
var="apple"
fi
ssh test@$i "uname -n"
done > /tempout/uname_${var}.txt
我将文件名为/tempout/uname_..txt
预期的文件名应该是uname_apple.txt
我有一个简短的bash脚本来检查Python程序是否正在运行。该程序在运行时会写出一个PID文件,因此将其与当前正在运行的进程列表进行比较,可以满足我的需要。但我有一个问题,一个变量被改变,然后明显地改变回来!下面是剧本:
#!/bin/bash
# Test whether Home Server is currently running
PIDFILE=/tmp/montSvr.pid
isRunning=0
# does a pid file exist?
if [ -f "$PIDFILE" ]; then
# pid file exists
# now get
bash脚本
for i in $script_name
do
echo -en "running the script - $i\t - "
exec 3>&1 4>&2
var=$( { time /tmp/scripts/$i 1>&3 2>&4; } 2>&1) # Captures time only
exec 3>&- 4>&-
echo "$var"
done
打印以下内容:
running the script - Verif
我从https://unix.stackexchange.com/a/175845/674上读到一条命令
为什么
$ bash -c 'for f do echo "$f";done' bash a b c
a
b
c
输出与
$ bash -c 'for f in "$@"; do echo "$f";done' bash a b c
a
b
c
?
为什么for-循环不要求显式指定
循环变量的一组值,如in "$@"和
;?
谢谢。
这是我使用的脚本的简化版本:在其简化版本中,tt应逐行读取文件input,然后将其打印到标准输出,并写入文件log。
input文件:
asas
haha
asha
hxa
脚本(名为simple):
#!/bin/bash
FILE=input
logfile="log"
exec > >(tee "$logfile") # redirect the output to a file but keep it on stdout
exec 2>&1
DONE=false
until $DONE; do
read || DONE
我想用逗号分隔字符串,然后把它写到一个文件中。我有以下脚本,但是当我看到输出(temp.txt)时,我会看到完整的for循环代码写入文件。有人能帮我解决这个问题吗?
#!/bin/bash -e
string1=a/b,b/c,c/d
IFS=', ' read -a array <<< "$string1"
cat << 'EOF' > temp.txt
for element in "${array[@]}"
do
echo "$element"
done
EO
我正在尝试编写一个bash脚本,它将读取.txt文件中的一系列ip地址,并使用NSE ()脚本对每个ip执行nmap扫描。在这个输出中,我只想打印特定的线条,但前提是其中一条与特定的模式匹配。
我尝试了多个不同的选项,但无法让它按我的意愿工作,因为我想检查和输出的两个项目在两个不同的行中。我最接近的是编写了下面的bash脚本:
#!/bin/bash
for server in $(cat servers-smb.txt); do
nmap --script smb-os-discovery $server | grep "report\|OS: Windows"
done
在
今天,我在linux中尝试了一个脚本,将所有文件放在一个目录中。这很简单,但我发现了一些有趣的东西。
#!/bin/bash
InputDir=/home/XXX/
for file in $InputDir'*'
do
echo $file
done
输出为:
/home/XXX/fileA /home/XXX/fileB
但是当我直接输入dir时,就像这样:
#!/bin/bash
InputDir=/home/XXX/
for file in /home/XXX/*
do
echo $file
done
输出为:
/home/XXX/fileA
/home/
我有以下bash脚本: #!/bin/bash
items=('mysql_apache','postgresql_apache','maria_apache')
string=""
for i in "${array[@]}"; do
string=$string" -t"$i
done
echo $string 但是如果我输出字符串,我不会得到预期的结果: -t 'mysql_apache' -t 'postgresql_apache' -t
我对shell脚本很陌生,我正在尝试在ubuntu脚本中学习while循环。Script.sh
#!/bin/bash
#Initializing two variables
#while loop
echo while loop
a=0
while [ $a -lt 10 ]
do
echo $a
a=$(a + 1)
done
输出是-
vscoder@vscoder-VirtualBox:~$ ./script.sh
while loop
0
./script.sh: line 10: a: command not found
./script.sh: line 7: [: -lt
我有一个kubernetes命令kubectl get pods,它将这个输出打印到终端
NAME READY STATUS RESTARTS AGE
redis-cart-74594bd569-ffrhk 1/1 Running 0 40m
我希望这个命令在bash脚本中每两秒钟重复运行一次。但是,我希望输出覆盖最后一个输入。
目前,我可以通过使用回车使其覆盖一行(第一个输出行)。但它并没有覆盖第二行。
如何覆盖先前从bash脚本输出的文本?
按脚本看,就像这个自动取款机
#!/bin/bas
每当我执行输出多行的linux命令时,我都想对输出的每一行执行一些操作。一般来说,我是这样做的
command something | while read a
do
some operation on $a;
done
这可以很好地工作。但我的问题是,有没有一些方法可以通过预定义的符号(不知道如何调用它)来访问每一行?/像$这样的东西?或者..。$!..或者..。$_
有没有可能做
cat to_be_removed.txt | rm -f $LINE
bash中是否有预定义的$LINE ..或者前一条是最短的路。即。
cat to_be_removed.txt | while read