例如:
# case 1
for i in "a b c"; do echo -n $i| od -b; done
# case 2
v="a b c"; for i in $v; do echo -n $i| od -b; done
输出:
0000000 141 040 142 040 143
0000005
0000000 141
0000001
0000000 142
0000001
0000000 143
0000001
为什么for in不能在bash中直接拆分字符串?例1和例2有什么区别?
我正在创建一个bash脚本来获取cpu%,pps &传入kbps。
#!/bin/bash
INTERVAL="0.5" # update interval in seconds
IFS="enp0s3"
while true
do
# Read /proc/stat file (for first datapoint)
read cpu user nice system idle iowait irq softirq steal guest< /proc/stat
# c
我正在尝试启动一个命令,它可以用Bash启动,但不能用Fish启动:
On Bash --工作得很好:
$ sudo strace -f -s3000 -p $(pgrep -f teams -d " -p ") -o /tmp/debug.log
strace: Process 49774 attached with 32 threads
strace: Process 49778 attached
strace: Process 49780 attached
strace: Process 49817 attached with 10 threads
strace: Pro
关于Kubuntu 15.10
echo $BASH_VERSION
4.3.42(1)-release
我试着
reps=1
threads={1}{10..60..10}
for((r=0;r<$reps;r++))
do
tCount=0
for t in $threads
do
echo "t=$t, tCount=${tCount}"
#do something funny with it
((tCount++))
done
done
它产生一条单线
t={1}{10..60..1
#!/bin/bash
show="ls -al /"
IFS=$'\n'
$show
结果就像bash: ls -al /: No such file or directory一样。
外壳程序无法返回预期结果。
如果我将IFS更改为$' \n' (注意,我添加了一个空格),就可以了。
我对IFS了解不多,有人能解释一下吗?
在zsh中,是什么控制环境被空间分割?
我相信这是很简单的事情,但在我所有的搜索中,还没有弄清楚是什么控制了它。
试图在空格分隔的字符串中循环项,如下所示:
s='foo bar baz'
for i in $s; do
echo "$i END"
done
# foo bar baz END
# ---
s='foo bar baz'
a=( $s )
echo ${a[0]} # (empty)
echo ${a[1]} # foo bar baz
# ---
s='foo bar baz'
IFS=' &
在bash中,为什么这样做很好:
$ cat test1.sh
#!/bin/bash
echo "some text" \
"some more text"
$ ./test1.sh
some text some more text
但这是失败的
$ cat test2.sh
#!/bin/bash
text="some text" \
"some more text"
echo $text
$ ./test2.sh
./test2.sh: line 3: some more text: command not found
我原
我想从bash脚本中写一些颜色的文本,但是字符串" -s, --with-sequence"没有被打印出来。
local rc="\e[0m"
local bfcode="\033[0;49;33m"
local frmt="${bfcode} %s ${rc}\n"
local _PC="printf $frmt"
$_PC " -s, --with-sequence"
我希望从user获取输入,并将它们存储在一个数组中,然后反转该数组。
例如:如果我输入“我是一个初学者”,它必须显示"rennigbe a ma i“。这是我的代码。
这段代码中唯一的问题是它只反转一个字符串,而不是整个数组。
例如,我输入了"windows“。它显示的是"swodniw“。但是当我输入“我是一个初学者”时,它只显示“我”。
为什么会这样呢?我不熟悉bash。请帮帮我。
#!/bin/bash
echo "type:"
read -a input
for a in ${input[@]}
do
echo "you typed:$
我想逐行地将文件读入脚本。文件中的每一行都是由制表符分隔的多个值,我想将每一行读入一个数组。
典型的bash“逐行读文件”示例;
while read line
do
echo $line;
done < "myfile"
对我来说,myfile看起来像这样(制表符分隔值);
value1 value2 value3
value4 value5 value6
在循环的每一次迭代中,我希望每一行都进入一个数组,这样我就可以
while read line into myArray
do
echo myArray[0]
echo myArray[1]
echo myAr
我有一个bash脚本,我在Debian 11上运行,它运行得很好,我必须将它移到Ubuntu (可能是22.04),现在它停止工作了。我的剧本如下:
BRANCHES=("master" "develop")
for BRANCH in "${BRANCHES[*]}"
do
echo Building $BRANCH
git checkout "$BRANCH"
#Code doing tasks
done
还尝试了“${分支}”,它给了我输出:
Building ("master" "
PhoneBook.txt有由:分隔的名称
ex= first:last:number - simple
ex= first first:last last:number - 2 firstnames and/or 2 last names
ex= f'irst:l'ast:number - first name or last name with a single quote in it.
我的当前脚本
#!/bin/bash
firstName="$1"
lastName="$2"
if [[ &
我一直在跟进这些问题的答案。
编写bash脚本,它遍历文件夹中的文件并对其进行处理。下面是我的代码:
#!/bin/bash
YEAR="2002/"
INFOLDER="/local/data/datasets/Convergence/"
for f in "$INFOLDER$YEAR*.mdb";
do
echo $f
absname=$INFOLDER$YEAR$(basename $f)
# ... the rest of the script ...
done
我收到了这个错误:basena
给定python脚本
if __name__ == '__main__':
print("first")
print("second")
print("third")
bash脚本
#!/usr/bin/env bash
declare -a choice=$( python3 test.py )
echo "You chose "
for c in "${choice[@]}"; do
echo "> ${c}"
done
应打印
You c
我试图使用bash脚本显示某些细节,但是bash脚本的输出与终端的输出不同。
终端输出:
ubuntu@ubuntu:~/ubin$ cat schedule.text | grep 09/06/12
Sat 09/06/12 Russia 00:15 Czech Republic A
Sat 09/06/12 Netherlands 21:30 Denmark B
ubuntu@ubuntu:~/ubin$
Bash脚本输出:
ubuntu@ubuntu:~/ubin$ bash fixture.sh
Sat 09/06/12 Rus
这在bash中如预期的那样工作:
> t="ls -l"
> $t #== ls -l
> "$t" #== "ls -l"
ls -l: command not found
但在zsh,我得到了这个:
> t="ls -l"
> $t #== "ls -l"
ls -l: command not found
如何强制shell像bash那样再次解析变量值?