我一直在编写一个bash脚本,它应该循环遍历您应该拥有的用户列表和系统上的用户列表。我目前正在尝试将存储在变量中的用户名添加到数组(list)的末尾。下面是我的代码正在做的事情:
for x in array
do
for y in otherArray
do
if [[ $x == $y ]]
then
newArray += $x
elif [[ $x != $y ]]
then
otherNewArray += $x
fi
done
do
我问题的题目有点模糊。现在让我清楚地解释一下。
我们知道bash中有一个"MAILCHECK“,每隔几分钟bash就会检查邮箱,如果您有新邮件,就会给您一条消息。请注意,此通知不需要命令。如果有新邮件,任何时候都可以自动打印邮件。
在这里,我有几个问题:
在我的zsh中没有这样的通知(也许我忘记了我的.zshrc中的一些东西)
如何更改bash/zsh中的“新邮件通知”格式
在bash/zsh中完成我的任何命令之后,如何执行a certain command。例如,当我键入ls和<enter>时,ls将被执行,然后the certain command将被执行。
$ jq -c -n -e '[$x, $y]' --argjson x '"a"' --argjson y '"b"'
["a","b"]
我知道我可以做上述的事情。如果我想从bash数组生成一个json数组,如下所示。是否有办法通过任意长度的论证来做到这一点?
$ jq {some jq arguments} a b c # the bash array can be of an arbitrary length.
["a","b","c&
在bash中,我们可以迭代这样的数组的索引
~$ for i in "${!test[@]}"; do echo $i; done
比如说,测试是一个数组,
~$ test=(a "b c d" e f)
所以输出看起来就像
0
1
2
3
但是,当我在zsh中做同样的操作时,我会得到一个错误:
➜ ~ for i in "${!test[@]}"; do echo $i; done
zsh: event not found: test[@]
怎么一回事?
在zsh中对索引进行迭代的正确方法是什么?
对这个问题的扩展:
将命令的参数存储为bash数组之后
touch "some file.txt"
file="some file.txt"
# Create an array with two elements; the second element contains whitespace
args=( -name "$file" )
# Expand the array to two separate words; the second word contains whitespace.
find . "${args[@]}"
下面的代码用于查找~/Downloads中的子目录。我和. ./script.sh一起运行它。即使用户提交了一个不完整的名称,它也会找到它们。
#!/usr/bin/bash
echo -e "\nGive me the name of a nonexitent directory and I will look for it in ~/Downloads?\n"
read word
TOGOs=("$(find ~/Downloads -maxdepth 1 -iname "*$word*" -type d -execdir echo {} +
要访问数组元素,@和$工作。例如:
@item_price_list = (5 , 8 , 24);
@item_name_list = ("Apple", "Banana", "Mushroom");
print "The price of one @item_name_list[1] is @item_price_list[1] gold coins.\n";
print "The price of one $item_name_list[1] is $item_price_list[1] gold coins.\n&
我希望使用tr命令将字符映射到新的字符,例如:
echo "hello" | tr '[a-z]' '[b-za-b]'将输出:ifmmp
(将小写字母中的每个字母移到右边的一个字母上)
参见下面对'[b-za-b]'的新字符的映射
[a b c d e f g h i j k l m n o p q r s t u v w x y z]将映射到:
[b c d e f g h i j k l m n o p q r s t u v w x y z a]
但是,当我希望它多次旋转时,如何使用变量来控制tr命令的旋
发布
如何将grep的整行输出存储为1变量,而不是每个字符串。
示例(我只需要3个变量,整行)。
user@local:~/bin/kb$ grep -E '##.*bash.*file.*add' bash.kb
## bash, file, add string behind founded string
## bash, files, add string to begin
## bash, file, add comma to end of line except last line
user@local:~/bin/kb$
但是for的例子。
user@local:~
我试图通过Dockerfile中的CMD" bash“、" start.bash”执行名为start.bash的bash文件。当我创建映像时,即使bash文件在dockerfile中被正确复制,也由于某种原因没有执行该命令。关键是,当im试图在容器本身内运行这个命令时,它成功了。
这是我的Dockerfile:
# build back end
FROM node:12.22.12 AS server_build
###ENV HOSTNAME myhost
WORKDIR /VideoServiceApp
COPY ./projConf.json /VideoServi
在编写bash脚本时,我遇到了一个奇怪的问题:一个具有特定名称的数组具有不可预测的长度,这在某种程度上取决于它的输入。所有其他数组的行为都是正确的。这就是我能观察到的:
问题仍然存在,如果我将该数组导出到其他脚本中----如果我重命名该数组,那么如果在其他脚本中生成同名的数组,问题就会消失,它的行为将正确地执行。
可能是某种内存分配错误,因为存在一个完全相同名称的变量,它不是数组。
以下是一些奇怪的例子:
原始输入
ARRAY=(a b d f k l)
bash输出
$ bash setup-env.sh
Array:
a
原始输入
ARRAY=(asdfasdf1 asdasdf2 asd
我想做一些类似于包的事情。通常用于回答所有提示的"y“。
想象一下,有一个程序(script_that_prompts.sh)提供了数千个提示。
for ((n=0;n<3000;n++))
do
read -p "input 0|1|2|y:" ans;
# do something with ans
# ...
done
I需要一个命令,从列表'0‘、'1’、'2‘、'y’中随机选择一个答案,并为循环中的每个提示符响应提示符。
我已经阅读了yes包的手册,它似乎只接受一个字符串(和选项)。它只能用一
我得到了在Bash脚本中使用而不是ls的提示,在我的代码中,我按照说明将array=($(ls))替换为:
function list_files() { for f in *; do [[ -e $f ]] || continue done }
array=($(list_files))
然而,新函数没有返回任何东西,我是不是做错了什么?