我在终端中运行此命令:
grep "bla bla blah" blah* | echo "Blah: $(wc -l) / $(ls | wc -l) * 100"
Run Code Online (Sandbox Code Playgroud)
我得到这个输出:
Blah: 44 / 89 * 100
Run Code Online (Sandbox Code Playgroud)
我期望看到什么:
49.4
Run Code Online (Sandbox Code Playgroud)
有没有办法仅使用 bash 命令来获得所需的输出?我不喜欢我计划通过管道输出的脚本。
我正在尝试调试其他人的脚本:
代码行是:
y=$((${oldvalue[$x]}-${newvalue[$x]}))
Run Code Online (Sandbox Code Playgroud)
y只要双方都是正数,就可以很好地计算。但是,我有一个生产情况,它们都是负面的,我得到的错误是:
DEBUG Old value = -4144290000
DEBUG New value = -4009685000
script.sh: line 123: -4144290000--4009685000: assignment requires lvalue
Run Code Online (Sandbox Code Playgroud)
ksh即使是最简单的计算,我也不会使用自己,但我处于生产支持的位置并且必须处理一大堆泥浆,我至少会使用 Perl/Python。谁能告诉为什么会发生这个问题以及如何解决它?
我需要减去shell中时间格式的两行。时间格式是 hh:mm:ss 我使用下面的代码来获取时间。
cat /var/log/kern.log |grep usb |tail -2| awk '{print $3}'
Run Code Online (Sandbox Code Playgroud)
上面代码的输出是
18:23:24
18:20:20
Run Code Online (Sandbox Code Playgroud)
如何在几秒钟内找到差异?
我有一个表达式,"5+50*3/20 + (19*2)/7"我需要将它四舍五入到小数点后 3 位。答案是17.92857142857143。当我使用下面的脚本时,它给了我17.928. 答案应该是17.929。
read exp
echo "scale=3; $exp" |bc -l
Run Code Online (Sandbox Code Playgroud)
还有一个问题是如何使用printf来完成相同的任务
在 Bash Manual, sec 6.5 Shell Arithmetic
expr1 , expr2
comma
Run Code Online (Sandbox Code Playgroud)
逗号运算符有什么作用?
是expr1和expr2算术表达式吗?
我需要加速一个脚本,该脚本基本上确定每行的所有“列”是否相同,然后写入一个包含相同元素之一或“no_match”的新文件。该文件以逗号分隔,由大约 15,000 行组成,并包含不同数量的“列”。
例如:
1-69
4-59,4-59,4-59,4-61,4-61,4-61
1-46,1-46
4-59,4-59,4-59,4-61,4-61,4-61
6-1,6-1
5-51,5-51
4-59,4-59
Run Code Online (Sandbox Code Playgroud)
写入一个新文件:
1-69
no_match
1-46
no_match
6-1
5-51
4-59
Run Code Online (Sandbox Code Playgroud)
删除第二行和第四行,因为它们包含不同的列。
这是我远非优雅的脚本:
#!/bin/bash
ind=$1 #file in
num=`wc -l "$ind"|cut -d' ' -f1` #number of lines in 'file in'
echo "alleles" > same_alleles.txt #new file to write to
#loop over every line of 'file in'
for (( i =2; i <= "$num"; i++));do
#take first column of row being looped over (string to check match of other columns with)
match=`awk "FNR=="$i" {print}" …Run Code Online (Sandbox Code Playgroud) 我有一个变量集,其中的数字用空格分隔,其中第一个数字也可以由空格引导,例如:
VAR=" 2 1 34 3 2 "
Run Code Online (Sandbox Code Playgroud)
我需要将所有这些数字相加。最简单的方法是将数字之间的所有空格替换为+bc 上的管道
我可以用 for 循环、粘贴和 bc 来完成,但也许有人知道更简单的方法?也许直接在 bash 中VAR使用 bash 内置字符串替换进行一些计算?
$ for i in $VAR;do echo $i;done|paste -sd+|bc
42
Run Code Online (Sandbox Code Playgroud)
更新:感谢所有建议,我终于找到了带有数组的相当短的方法:
$ VAR=" 2 1 34 3 2 "
$ arr=( $VAR );echo "$((${arr[@]/%/+}0))"
42
$ VAR="$VAR -14"
$ arr=( $VAR );echo "$((${arr[@]/%/+}0))"
28
$
Run Code Online (Sandbox Code Playgroud) ZSH 手册 ( zshparam(1)) 写道:
Array Subscripts
Individual elements of an array may be selected using a subscript. A
subscript of the form `[exp]' selects the single element exp, where
exp is an arithmetic expression which will be subject to arithmetic
expansion as if it were surrounded by `$((...))'.
Run Code Online (Sandbox Code Playgroud)
然而,这很快就失败了:
mc% arr=(a b c d e)
mc% echo $arr[$#arr]
e
mc% echo $arr[$(($#arr))]
e
mc% echo $arr[$(($#arr - 1))]
d
mc% echo $arr[$#arr - 1]
zsh: invalid subscript
Run Code Online (Sandbox Code Playgroud)
问题: …
我以纳秒精度获取日期:
$ start=$(date '+%s.%N')
Run Code Online (Sandbox Code Playgroud)
...然后打印它:
$ echo ${start}
1662664850.030126174
Run Code Online (Sandbox Code Playgroud)
到目前为止,一切都很好。但是看看当我以任意大的精度 printf 时得到的结果:
1662664850.0301261739805340766906738281250000000000000000000000000
Run Code Online (Sandbox Code Playgroud)
Q1. date 命令实际上是否用那么多信息填充了 start 变量,或者这些数字只是垃圾?
这是问题的第二部分。假设我想做一些时间数学。创建一个“结束”时间戳:
$ end=$(date '+%s.%N')
$ echo ${end}
1662665413.471669572
$ printf "%.55f\n" ${end}
1662665413.4716695720562711358070373535156250000000000000000000000
Run Code Online (Sandbox Code Playgroud)
现在我使用 bc 得到了我期望的结果:
$ echo $(bc <<< ${end}-${start})
563.441543398
Run Code Online (Sandbox Code Playgroud)
但看看我使用 python 或 perl 时得到的结果:
$ echo $(python -c "print(${end} - ${start})")
563.441543579
$ echo $(perl -e "print(${end} - ${start})")
563.441543579102
Run Code Online (Sandbox Code Playgroud)
在某个时刻,这个数字会脱离轨道:
BC 563.441543 398 python 563.441543 579 perl 563.441543 579 102
Q2。这些数字有所不同,但由于四舍五入而导致的结果与您预期的不同。是什么赋予了?
系统信息:
Linux 3.10.0-1160.71.1.el7.x86_64 #1 SMP 2022 年 …
Bash中有两种定义和使用整型变量的方法
declare -i 一个新变量我的问题:
两种方式创建的变量有什么区别?尤其是它们的用途不同,什么时候使用哪个?