以交互方式运行后sleep 10 &
立即关闭运行该命令的终端,该sleep
命令将在 10 秒之前终止。
但是当这个命令被放入脚本时:
# this is a file named testfile.sh
sleep 10 &
然后作为执行bash testfile.sh
,sleep
即使终端关闭后,命令仍会持续执行直至完成。
为什么在一种情况下sleep
终端关闭后立即停止,而在另一种情况下它继续执行?
以交互方式运行后sleep 10 &
立即关闭运行该命令的终端,该sleep
命令将在 10 秒之前终止。
但是当这个命令被放入脚本时:
# this is a file named testfile.sh
sleep 10 &
然后作为执行bash testfile.sh
,sleep
即使终端关闭后,命令仍会持续执行直至完成。
为什么在一种情况下sleep
终端关闭后立即停止,而在另一种情况下它继续执行?
在日志中搜索的最简单的交互式脚本
#!/bin/bash
# Starting - sh /tmp/czr.sh
printf "1 - Option 1\n2 - Option 2\n";
read -r select
if [ $select = "1" ] ; then
echo "Option 1 do nothing" ;
fi
if [ $select = "2" ] ; then
echo -n "Type what to find: "
read -r typed
cat /var/log/httpd/maps_error_log | grep -i "$typed" --color
fi
exit
sh
我想启动一个预定义选项 2 的
echo "2" | sh /tmp/czr.sh
但是这样的命令没有提供输入我想要查找的内容的选项 - 它只是打开整个日志文件。
(好像 echo“2”不仅传递了“2 - 选项 2”的选择,还传递了“Enter”命令)。
是否可以使用预选选项 2 来启动上面的 bash,但仍允许输入我想要查找的内容(保存交互性)?
我想用 mikefarah/yq 将一个 k8s 资源文件拆分成每个资源的独立文件。显然,新文件的目标文件名必须是动态的。
K8s 资源的一个非常明显的命名方案是 [kind-name]。所以我想到了这个命令。
yq '.items[]' my-file.yaml -s '.kind + "-" + .metadata.name'
但是 K8S 名称可能包含文件名中不允许的字符,例如冒号。如果发生这种情况,我会收到此错误
错误:打开 ClusterRole-system:[...].yml:文件名、目录名或卷标语法不正确。
有没有办法替换动态目标文件名中的字符?
我可以在 Bash 中的命令之间来回切换:
$ vim
CTRL-Z
# vim is in background now
# can resume it with:
$ fg
我试图用嵌套的 shell 做同样的事情,但是没有效果:
# launch nested Bash shell:
$ bash
CTRL-Z
# still in nested shell...
是否可以像示例一样将嵌套的shell置于后台并返回到父shell vim
?
如果不可能,那么原因是什么?
首先,我获取一个 base64 编码的字符串并对其进行解码:
local base64_str="OQbb8rXnj/DwvglW018uP/1tqldwiJMbjxBhX7ZqwTw="
echo "${base64_str}" | base64 --decode > foo.txt
二进制文件的大小为 32 字节,基于:wc -c < foo.txt
我使用xxd
将文件中的值编码为十六进制格式:
xxd -p ./foo.txt ./foo.hex.txt
文件 foo.hex.txt 中的十六进制值为:
3906dbf2b5e78ff0f0be0956d35f2e3ffd6daa577088931b8f10615fb66a
c13c
编码哈希文件的大小为 66 字节,使用wc -c < foo.hex.txt
我想将 base64 字符串转换为十六进制,使其保留为 32 字节字符串,以便我可以与 openssl 一起使用 aes-256 密码进行加密和解密。
local iv_hex=$(base64_to_hex "${iv}")
local key_hex=$(base64_to_hex "${key}")
openssl enc -aes-256-ctr -K "${key_hex}" -iv "${iv_hex}" -in "${input_file}" -out "${output_file}"
我(拼命地)尝试在 podman 中复制此 mlflow/minio 设置:https://github.com/minio/blog-assets/blob/main/mlflow-minio-setup/docker-compose.yml。本教程在 VM 中使用 docker-compose 运行良好。
然而,在 podman 中,我根本无法弄清楚如何执行脚本以在 minio 中自动创建存储桶。我尝试了 --entrypoint 选项的许多可能组合,但总是失败:Error: crun: executable file [/bin/sh,-c,/create_bucket.sh] not found in $PATH: No such file or directory: OCI runtime attempted to invoke a command that was not found
...
创建存储桶:
#!/bin/sh
sleep 5;
/usr/bin/mc config host add mlflow_minio_storage http://mlflow_minio_storage:9000 "$MINIO_ACCESS_KEY" "$MINIO_SECRET_ACCESS_KEY" --api S3v4;
/usr/bin/mc ls mlflow_minio_storage | grep -q challenge || /usr/bin/mc mb mlflow_minio_storage/mlflow;
/usr/bin/mc policy download mlflow_minio_storage/mlflow;
exit 0;
Dockerfile:
FROM docker.io/minio/mc
COPY create_bucket.sh /create_bucket.sh
RUN chmod +x /create_bucket.sh
podman 运行:
podman build -t minio_client:latest -f Containerfile
podman run \
--name=mlflow_minio_client \
--detach \
--secret=MINIO_ACCESS_KEY,type=env,target=MINIO_ACCESS_KEY \
--secret=MINIO_SECRET_ACCESS_KEY,type=env,target=MINIO_SECRET_ACCESS_KEY \
--net mlflow_net \
--network-alias mlflow_minio_client \
--restart always \
--requires mlflow_minio_storage \
--entrypoint ["/bin/sh","-c","/create_bucket.sh"] \
localhost/minio_client:latest
即使这种简单的情况也会导致相同的错误。我是不是误解了什么?
podman run \
--entrypoint ["/bin/sh","-c","/usr/bin/mc version"] \
docker.io/minio/mc:latest
>>> Error: crun: executable file `[/bin/sh,-c,/usr/bin/mc version]` not found in $PATH: No such file or directory: OCI runtime attempted to invoke a command that was not found
我试图理解为什么我似乎无法成功实现用于flock
避免竞争条件的计数器。
我flock
从这个 SO 答案中复制了代码。
计数器似乎会随机重置,尽管我不明白为什么它会这样做。counter inc
应该永远自动计数。
我的代码旨在让我能够使用同时处理多个文件GNU parallel
,但是使用包含递增计数器的每个处理文件来更新状态行。
该计数器保存到由 创建的 RAM 中的临时文件中mktemp
。
我在这里把它剪短了,所以它只是一个应该永远计数的无限循环,但它会不断重置。
谁能向我解释为什么计数器有时会重置?
#!/bin/bash
clear_line () {
echo -ne "\r\033[K"
}
counter () {
{
flock -s 200
read numfiles < "$countfile"
if [ "$1" = "inc" ]; then
((numfiles++))
fi
if [ "$1" = "rst" ]; then
numfiles=0
fi
if [ "$1" = "get" ]; then
echo "$numfiles"
fi
echo "$numfiles" > "$countfile"
} 200> "$countlockfile"
}
process () {
counter inc
currfileno=$(counter get)
clear_line
echo -n "$currfileno"
}
# from https://unix.stackexchange.com/a/29918/52247
tmpdir=
cleanup () {
trap - EXIT
if [ -n "$tmpdir" ] ; then rm -rf "$tmpdir"; fi
if [ -n "$1" ]; then trap - $1; kill -$1 $$; fi
}
tmpdir=$(mktemp -dt "$(basename $0).XXXXXXXX" --tmpdir=/run/user/$(id -u)) || exit 1
countfile=$(mktemp -t "counter.XXXXXXXX" --tmpdir=$tmpdir) || exit 1
countlockfile=$(mktemp -t "countlockfile.XXXXXXXX" --tmpdir=$tmpdir) || exit 1
trap 'cleanup' EXIT
trap 'cleanup HUP' HUP
trap 'cleanup TERM' TERM
trap 'cleanup INT' INT
export -f clear_line
export -f process
export -f counter
export countfile
export countlockfile
counter rst
while :
do
echo whatever
done | parallel process
我有这个脚本,它似乎printf '%s\0%s\0'
以代码 1 退出,我不明白为什么。
我甚至强迫它使用 bash 版本 5.2 - 结果相同。
以下是示例脚本及其输出
#!/usr/bin/env bash
echo $BASH_VERSION
set -x
set -Eeuo pipefail
function prompt_creds {
local username password
read -rp "username " username
read -rsp "password " password
printf '%s\0%s\0' "${username}" "${password}"
}
function main() {
IFS=$'\0' read -r username password < <(prompt_creds )
echo "name name is ${username}" "my password is ${password}"
}
main
使用和输出
./sample.sh
3.2.57(1)-release
+ set -Eeuo pipefail
+ main
+ IFS=
+ read -r username password
++ prompt_creds
++ local username password
++ read -rp 'username ' username
username user
++ read -rsp 'password ' password
password ++ printf '%s\0%s\0' user pass
bash-5.2$ echo $?
1
我甚至尝试在 CLI 上简单运行它,并且它可以工作:
bash-5.2$ IFS=$'\0' read -r u p < <(printf '%s\0%s\0' user pass)
bash-5.2$ echo $u $p
userpass
我需要使用 bash 修改 csv 文件。
输入(csv 文件):
firstletter="s"
surname="houston"
emaildomain"@zzz.com"
input=$(cat 1.csv)
1.csv:
1,1,Susan houston,Director of Services,,
2,1,Christina Gonzalez,Director,,
3,2,Brenda brown,"Director, Second Career Services",,
如何使用 Linux bash 在最后两个逗号之间添加文本?我尝试了以下方法:
for i in $(cat $input);do
sed -i "s/,$/${firstletter}${surname}${emaildomain},/g" $i;
done
但是,这会导致错误:
sed: -e expression #1, char 5: unterminated `s' command
预期输出:
1,1,Susan houston,Director of Services,[email protected],
2,1,Christina Gonzalez,Director,[email protected],
3,2,Brenda brown,"Director, Second Career Services",[email protected],
在 ubuntu 上我运行以下脚本(为了测试目的,该脚本应该随机失败或成功):
if [ "$RANDOM" -gt 16384 ]; then
echo "Success!"
exit 0
else
echo "Failure!"
exit 1
fi
和
sh random.sh
但它失败了
random.sh: 1: [: Illegal number:
Failure!
问题的原因是什么?