刚刚切换到使用zsh (噢-天-Zsh),无法获得自动完成的工作。每当我打开一个新终端(使用iTerm和VSCode终端),就会将以下输出打印到终端上:
/usr/local/etc/bash_completion:59: command not found: shopt
/usr/local/etc/bash_completion:65: command not found: complete
/usr/local/etc/bash_completion:72: command not found: complete
/usr/local/etc/bash_completion:73: co
我希望将数组参数传递给bash中的函数,并编写一些测试代码如下:
#!/bin/sh
function foo {
a=$1;
for i in ${a[@]} ; do
echo $i
done
}
names=(jim jerry jeff)
foo ${names[@]}
上面的代码只显示jim,而不是三个j*。所以我的问题是:
为什么我的代码不能工作什么是正确的方法
我找到了在shell中使用getopt命令的示例脚本。
#!/bin/bash
args=$(getopt ab $*)
set -- $args
for i;
do
case "$i" in
-a)shift; echo "it was a";;
-b)shift; echo "it was b";;
esac;
done
它工作得很好,但是我不知道变量$i被赋值在哪里。它如何知道它必须遍历$arg。你能解释一下这个吗?
我的代码的概念类似于:
#include <stdio.h>
int main(int argc, char *argv[])
{
int num;
FILE *fp;
getint("num",&num); /* This line is pseudo-code. The first argument is key for argument, the second is the variable storing the input value */
fp = inputfile("input"); /* This line
#!/bin/bash
arrck=
arrrlt=
if true; then
arrck+=("1")
arrrlt+=("one")
else
echo "something"
fi
for i in ${!arrck[*]}
do
echo "${arrck[i]} is ${arrrlt[i]}"
done
output 1--
is
1 is one
output 2--tracing on
./Building_block.sh
+ arrck=
+ arrrlt=
+
我在windows上安装了git bash,并试着写几个简单的脚本。到目前为止,我还不能理解的一件事是简单的数学和比较。我现在需要的是一个随机的布尔值: #!/usr/bin/env bash
condition=$(($(($RANDOM%2)) == 1))
if $condition; then
echo "a"
else
echo "b"
fi 我找到command not found了。看起来git bash缺少比较运算符,无论我怎么尝试,<,>,==,!=,-eq,-lt,-gt都不能工作。我在所有的例子中都找到了这
我想在bash中创建一个map,其中一些map-key的值可能包含连字符(-)
我尝试了下面的代码
declare -a buckets
buckets["us-east-1"]="bucketname-us-east-1";
region="us-east-1"
buckets[$region]="bucketname-us-east-1";
# both of them throws buckets["us-east-2"]: bad array subscript
buckets["us\-e
我是bash脚本的新手,但我想在bash脚本中将一些文件设置为for循环中的变量。我有这样的代码:
a=home/my_directory/*.fasta
b=home/my_directory/*.aln
for i in {1..14} # I have 14 files in my_directory with file extension .fasta
do
clustalo -i $a -o $b # clustalo is a command of Clustal Omega software, -i is
# input fi
我正在尝试编写一个简单的bash脚本,该脚本将整数相加并提供和。我认为最简单的方法是将输入赋值给一个数组。然后遍历数组以执行求和。我需要在for循环中使用数组的长度,但不知道如何将数组长度赋给变量。
对简单的脚本(我用来学习bash)有什么帮助吗?
#!/bin/bash
# add1 : adding user supplied ints
echo -n "Please enter any number of integers: "
read -a input
echo "Your input is ${input[*]}"
echo "${#in
我一直在编写一个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
我正在尝试更改代码以从目录中读取.txt文件。该目录只包含一个.txt文件,但我事先不知道它的名称。我只知道它是一个.txt文件。使用shell脚本可以做到这一点吗?
下面是读取文件的当前代码,但我必须手动指定文件名。
#!/bin/bash
declare -a var
filename='file.txt'
let count=0
while read line; do
var[$count]=$line
((count++))
done < $filename