我有一个在 Linux 上运行的脚本,它接受一些参数。
我想做类似的事情:
if [[ $CONDITION == "true" ]]; then
script param1 --param2
else
script param1
fi
Run Code Online (Sandbox Code Playgroud)
我想避免if的分叉路径。
有没有更优化的方法来传递第二个参数?
我有一个 JSON 片段。
以下不起作用:
VALUE=<<PERSON
{
"type": "account",
"customer_id": "1234",
"customer_email": "jim@gmail.com"
}
PERSON
echo -n "$VALUE" | python -m json.tool
Run Code Online (Sandbox Code Playgroud)
结果是:
无法解码 JSON 对象
做同样的事情jq,即
echo -n "$VALUE" | jq '.'
Run Code Online (Sandbox Code Playgroud)
没有输出。
以下行为具有相同的行为:
VALUE=<<PERSON
'{
"type": "account",
"customer_id": "1234",
"customer_email": "jim@gmail.com"
}'
PERSON
echo -n "$VALUE" | python -m json.tool
Run Code Online (Sandbox Code Playgroud)
回复:
无法解码 JSON 对象
但以下工作:
VALUE='{
"type": "account",
"customer_id": "1234",
"customer_email": "jim@gmail.com"
}'
echo -n "$VALUE" | jq '.'
echo -n "$VALUE" | python -m json.tool
Run Code Online (Sandbox Code Playgroud) 我有一个函数,它取决于功能发生变化的参数。
我知道我可以做到:
function foo {
PARAM1=$1
PARAM2="$2"
VAR=$3
if[[ -z "$VAR" ]]; then
# code here
else
# other code here
fi
}
Run Code Online (Sandbox Code Playgroud)
我想知道是否有更合适的 bash 方法。这会奏效,但我不想有类似的东西
foo "x" "y" "blah"
foo "x" "y" "true"
foo "y" "y" "1"
Run Code Online (Sandbox Code Playgroud)
都是等价的。
有没有更适合 Bash 的方法?
有没有办法避免grep在文件中执行两次并一次性填充变量?文件很小,所以没什么大不了的,我只是想知道我是否可以一次性完成
FIRST_NAME=$(grep "$customer_id" customer-info|cut -f5 -d,)
LAST_NAME=$(grep "$customer_id" customer-info|cut -f6 -d,)
Run Code Online (Sandbox Code Playgroud) 我有一个包含换行符的文件。
我通过将文件发布curl到将其解析为 json 的服务器。
由于换行符,它拒绝请求。
但是当我这样做时:
$(echo "$MY_DATA" | sed 's/$//' | tr -d '\n\r')
Run Code Online (Sandbox Code Playgroud)
它有效,但新行字符不见了。
如何转义文本以保留换行符?
我试着tr '\n' '\\n'和sed 's/\n/\\n/g既不工作方法
我在 Linux 安装中有一个进程,它在某些时候有某种尖峰,并通过了系统允许的最大线程/进程数。我通过ps -elfT | wc -l反复检查发现了这一点。
但我不知道究竟是什么导致了这个峰值。
的输出ps -elfT有很多信息,但我无法轻易理解是否有一些子进程在分叉时做了某种“模糊”并弄得一团糟。
我怎么能弄明白呢?
示例:ps -elfT | cut -d' ' -f3 | sort |uniq给我当时正在运行的进程。我如何添加计数以查看每个计数对总数的贡献?
所以我正在测试以下内容:
foo() {
printf "\nAll the parameters, each on a separate line:\n"
printf "param: %s\n" "$@"
}
foo The "nicely colored" rainbow
Run Code Online (Sandbox Code Playgroud)
输出是:
All the parameters:
param: The
param: nicely colored
param: rainbow
Run Code Online (Sandbox Code Playgroud)
所以如果我理解正确,因为IFS设置为\t\n我们得到由制表符分隔的参数( 的第一个字符IFS)。
但是为什么它们被打印在不同的行中?
是为每个参数运行 printf 。即 bash 是否将其转换为 for 循环?
以下(不带双引号)也输出相同的结果:
printf "param: %s\n" $@
那么为什么以下工作会打印出匹配项:
THE_REGEX='^test\/version[0-9]+([.][0-9]+)+$'
if [[ "$SOME_VAR" =~ $THE_REGEX ]]; then
echo "Match!"
fi
Run Code Online (Sandbox Code Playgroud)
但以下不:
if [[ "$SOME_VAR" =~ '^test\/version[0-9]+([.][0-9]+)+$' ]]; then
echo "Match!"
fi
Run Code Online (Sandbox Code Playgroud)
有什么不同?这是相同的正则表达式
我有一个接受参数的脚本,如果未设置,则使用默认值。
VALUE=$1
if [[ -z "$VALUE" ]];
VALUE=foo #some function returns the default
fi
Run Code Online (Sandbox Code Playgroud)
然后脚本继续做一些事情。
我想“增强”它并提供一些dry run选择。因此,如果通过了,我们将有类似的东西
if [[ -z "$DRY" ]];
echo "command x y z"
else
command "x" "y" "z"
fi
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到这一点?
我有一个文件test.xml(如下所示)。这个grep有效:
grep '<appointment-id internal:ref=1.2.3/>' test.xml
Run Code Online (Sandbox Code Playgroud)
输出是:
<appointment-id internal:ref=1.2.3/>
Run Code Online (Sandbox Code Playgroud)
但这失败了:
grep "$(grep '<appointment-id internal:ref=1.2.3/>' test.xml)" test.xml
Run Code Online (Sandbox Code Playgroud)
输出是:
grep: invalid character range
Run Code Online (Sandbox Code Playgroud)
内容test.xml:
<note>
<to>Jim</to>
<from>John</from>
<heading>Reminder</heading>
<body>Some text</body>
<appointment-id internal:ref=1.2.3/>
</note>
Run Code Online (Sandbox Code Playgroud)
我已将两个grep命令(简单命令和嵌套命令)放入带有set -x. 这是该脚本的精确副本?&?粘贴:
set -x
grep '<appointment-id internal:ref=1.2.3/>' test.xml
grep "$(grep '<appointment-id internal:ref=1.2.3/>' test.xml)" test.xml
exit 0
Run Code Online (Sandbox Code Playgroud)
这是它的输出:
++ grep '<appointment-id internal:ref=1.2.3/>' test.xml
<appointment-id internal:ref=1.2.3/>
+++ grep '<appointment-id internal:ref=1.2.3/>' test.xml
++ grep ' <appointment-id internal:ref=1.2.3/>' test.xml
grep: invalid character range
++ exit …Run Code Online (Sandbox Code Playgroud)