例子
while [ -n "$1" ]
do
something
done
Run Code Online (Sandbox Code Playgroud)
我可以写$1代替吗"$1"?user=alexander和和有什么区别
user="alexander"?谢谢
使用 Kubator 命令行回答我的问题:
#Function that shows the files having the same content in the current directory
showDuplicates (){
last_file=''
while read -r f1_hash f1_name; do
if [ "$last_file" != "$f1_hash" ]; then
echo "The following files have the exact same content :"
echo "$f1_name"
while read -r f2_hash f2_name; do
if [ "$f1_hash" == "$f2_hash" ] && [ "$f1_name" != "$f2_name" ]; then
echo "$f2_name"
fi
done < <(find ./ -maxdepth 1 -type f -print0 | xargs -0 md5sum | …Run Code Online (Sandbox Code Playgroud) 我们如何使用多线程将多个文件追加到单个文件中,我的每个文件都有10M的行。所以我想同时处理所有文件?
#!/bin/bash
appendFiles A.TXT &
appendFiles B.TXT &
appendFiles C.TXT &
wait
function appendFiles
{
while read -r line; do
echo $line >>final.txt
done < $1
}
Run Code Online (Sandbox Code Playgroud) 在命令行上,如果我键入
adb -s "" shell
Run Code Online (Sandbox Code Playgroud)
我有一个外壳,但是如果我尝试在bash中这样做:
#!/bin/bash
ADB_ID=""
adb -s ${ADB_ID} shell
Run Code Online (Sandbox Code Playgroud)
我得到一个错误。我了解它没有传递$ {ADB_ID}的任何内容。我尝试将引号转义,这导致它查找名为“”的设备,这是不正确的。我也尝试过使用单引号和单转义的引号,两者都是错误的。如何在bash脚本中传递等效的命令行?
我一直在开发一个示例 bash 程序来练习 bash 脚本编写,但每当我尝试使用正确的变量运行它时,它都会输出“[:=:预期的一元运算符”并退出。
#! /bin/bash
clear
i=""
P="PASSWORD"
echo "Please enter your password"
while [ $i = "PASSWORD" ]
do
read $i
done
Run Code Online (Sandbox Code Playgroud) 我想从 bash 递归地遍历文件夹中的每个文件,并对它们进行某种简单的操作。例如,更改权限,更改时间戳,使用 ImageMagick 调整图像大小等,您明白了。
我知道(像大多数初学者一样)如何在目录中做到这一点,但递归......?
$ for f in *.jpg ; do convert $f -scale 25% resized/$f ; done
Run Code Online (Sandbox Code Playgroud)
让我们保持简单。说,
$ for f in *; do touch $f; done
Run Code Online (Sandbox Code Playgroud) 我总是得到
-bash: warning: command substitution: ignored null byte in input
Run Code Online (Sandbox Code Playgroud)
执行以下代码时出现警告:
BW=`bc <<< "$(cat $TMP | grep -Pzo '(?<="outgoing_traffic": )(.*)(?=,)')/1024^3"`
Run Code Online (Sandbox Code Playgroud)
如果结果不为零,也会出现该警告。
我怎样才能避免这种情况?
当它确实为 0 时,我怎样才能跳过该错误?
更新我尝试了下面提供的解决方案:
tr -d '\0'
Run Code Online (Sandbox Code Playgroud)
但该代码不起作用:
BW=tr -d '\0' < `bc <<< "$(cat $TMP | grep -Pzo '(?<="outgoing_traffic": )(.*)(?=,)')/1024^3"`
Run Code Online (Sandbox Code Playgroud)
输出:
-bash: warning: command substitution: ignored null byte in input
-bash: `bc <<< "$(cat $TMP | grep -Pzo '(?<="outgoing_traffic": )(.*)(?=,)')/1024^3"`: No such file or directory
Run Code Online (Sandbox Code Playgroud)
有趣的侧面事实,它不是 0
BW=`bc <<< "$(cat $TMP | grep -Pzo '(?<="outgoing_traffic": )(.*)(?=,)')/1024^3"` …Run Code Online (Sandbox Code Playgroud) 作为 BASH 脚本的一部分,我需要使用输入参数启动一些 python 代码。我也想避免创建一个临时的 *.py 文件。
这是我到目前为止:
input=123
output=$(
python - <<'END_SCRIPT'
print("$input")
END_SCRIPT
)
echo $output
Run Code Online (Sandbox Code Playgroud)
此代码段将 python 调用的输出写入输出变量。但是它没有返回“123”,而是返回“$input”,因为在管道传输到 python 的过程中不会解释该变量。从 StdIn 读取时,有什么方法可以将输入变量作为参数转发给 python?
我尝试编写一个非常简单的 bash 脚本文件,名为bash_script.sh
#!/bin/bash
if [ -z $1 ];then
echo $1
else
echo 'no variable'
fi
Run Code Online (Sandbox Code Playgroud)
在终端中我尝试运行,./bash_script.sh hello但终端上的输出是no variable. 我在这里做错了什么?
我写了一个shell片段,如下所示:
is_first=t
for f in "$path"/"m*"
do
printf "%s\n" $f
printf "%s\n" $is_first
if [[ "$is_first" = "t" ]]; then
is_first=f
printf "%s\n" $is_first
else
printf "%s\n" $is_first
fi
printf "%s\n" $is_first
done
Run Code Online (Sandbox Code Playgroud)
输出如下:
./merge_non-null
./merge_non-null_bak.sh
./merge_non-null.sh
t
f
f
Run Code Online (Sandbox Code Playgroud)
我想知道,如何if-else在for循环中执行?它似乎只在第一个循环中运行,之后都被跳过.
另外,为什么printf "%s\n" $is_first只为第一个循环执行?
我期望的输出是(注意序列).我相信我错过了什么.对不起,如果它太愚蠢了.
./merge_non-null
t
f
f
./merge_non-null_bak.sh
f
f
f
./merge_non-null.sh
f
f
f
Run Code Online (Sandbox Code Playgroud) bash ×10
shell ×3
linux ×2
unix ×2
calculation ×1
command-line ×1
for-loop ×1
if-statement ×1
input ×1
parameters ×1
python ×1
sh ×1
variables ×1