标签: variable

使用变量值作为新变量名的一部分创建变量

我正在尝试使用现有变量的值作为变量名称的一部分来创建一个新变量。

filemsg"$word1"=" "
Run Code Online (Sandbox Code Playgroud)

我也试过

filemsg$word1=" "

filemsg${word1}=" "
Run Code Online (Sandbox Code Playgroud)

在所有尝试中,当该行执行时,我得到以下结果,

cicserrors.sh[45]: filemsgCICS= : not found [No such file or directory]
Run Code Online (Sandbox Code Playgroud)

shell variable

7
推荐指数
1
解决办法
2万
查看次数

尝试分配给非变量?

我们可以在 shell 脚本函数中使用算术运算:

function mess
{
   if (( "$1" > 0 )) ; then
      total=$1
   else
      total=100
   fi
   tail -$total /var/adm/messages | more
}
Run Code Online (Sandbox Code Playgroud)

我尝试对函数 args 执行此算术运算:

#!/bin/bash
byte="Bytes"
kilo="KB"
mega="MB"
giga="GB"
function bytesToUnites() {
if (( "$1" < 1000 )) 
then
    echo $1" "$byte
elif (( $1 < 1000000 ))
then
    let $1/=1000
    echo $1" "$kilo
fi
}

bytesToUnites 28888
Run Code Online (Sandbox Code Playgroud)

但我收到此错误:

line 12: let: 28888/=1000: attempted assignment to non-variable (error token is "/=1000")
28888 KB
Run Code Online (Sandbox Code Playgroud)

我怎样才能解决这个问题?

bash shell-script variable

7
推荐指数
1
解决办法
5474
查看次数

运行存储在 shell 变量中的命令

以下在我的 shell (zsh) 中有效:

> FOO='ls'
> $FOO
file1 file2
Run Code Online (Sandbox Code Playgroud)

但以下没有:

> FOO='emacs -nw'
> $FOO
zsh: command not found: emacs -nw
Run Code Online (Sandbox Code Playgroud)

即使emacs -nw直接调用Emacs 也能很好地打开。

为什么?

shell zsh quoting variable

7
推荐指数
2
解决办法
2969
查看次数

为什么while循环后数组为空?

我尝试在 bash-4.2 中初始化一个数组:

ring=()
ls -las | tail -n +4 | while read line
> do
> ring+=("$line")
> echo ${ring[-1]}
> done
3924 -rw-r--r-- 1 username group 4015716 Mar 23 15:14 script.jar
4 -rw-r--r-- 1 username group 9 Feb 29 12:40 rec.lst
5541 -rw-r--r-- 1 username group 5674226917 Mar 28 15:25 debug.out
8 -rw-r--r-- 1 username group 6135 Mar 25 12:16 script.class
8 -rw-r--r-- 1 username group 6377 Mar 25 11:57 script.java
8 -rwxr-xr-x 1 username group 4930 Mar …
Run Code Online (Sandbox Code Playgroud)

shell bash pipe variable

7
推荐指数
1
解决办法
1万
查看次数

bash shell 函数中的循环名称引用,但不在 ksh 中

我正在编写一组希望在 Bash 和 KornShell93 中使用的 shell 函数,但是使用 Bash 我遇到了“循环名称引用”警告。

这是问题的本质:

function set_it {
    typeset -n var="$1"

    var="hello:$var"
}

function call_it {
    typeset -n var="$1"

    set_it var
}

something="boff"
call_it something
echo "$something"
Run Code Online (Sandbox Code Playgroud)

运行它:

$ ksh script.sh
hello:boff

$ bash script.sh
script.sh: line 4: warning: var: circular name reference
hello:
Run Code Online (Sandbox Code Playgroud)

KornShell93 完全符合我的要求,但是 Bash 失败了,并且如果something脚本中的变量被命名,还会在第 2 行警告同样的事情var

我希望var变量是每个函数的局部变量,这就是我使用 的原因typeset,但 Bash 似乎不喜欢将 nameref “取消引用”到与 nameref 本身同名的变量。我不能使用,local -n或者declare -n因为它会在ksh缺少这些的情况下破裂,即使我这样做了,也不能解决问题。

我发现的唯一解决方案是 …

bash ksh function variable typeset

7
推荐指数
1
解决办法
1010
查看次数

bash- 使用 VAR=${[number]:-default} 定义变量

我遇到了一个使用的脚本

VAR1=${1:-8}

VAR2=${2:-4}

我可以从其他一些问题中看到并使用一些代码

VAR1=${VAR2:-8}

如果它存在,将创造VAR1任何价值VAR2。如果VAR2未设置,则VAR1默认值为 8,VAR2并将保持未设置。也就是说,在这个命令之后,echo VAR2不会返回任何东西。

那么,我的问题是第一行代码的作用。由于变量名不能以数字开头,VAR1显然没有被设置为 1 或任何名为 1 的变量。这肯定是有原因的,而且这不仅仅是一点毫无意义的混淆?

shell bash shell-script variable

7
推荐指数
2
解决办法
2万
查看次数

for循环中的Shell变量

我很难通过这段man bash话。

                          If the control variable in a for loop has the
nameref attribute, the list of words can be a list of shell  variables,
and  a name reference will be established for each word in the list, in
turn, when the loop is executed.  Array variables cannot be  given  the
-n attribute.  However, nameref variables can reference array variables
and subscripted array variables.
Run Code Online (Sandbox Code Playgroud)

你能给出一个循环中这个 nameref 变量的例子并解释一下吗?

bash for variable

7
推荐指数
1
解决办法
1116
查看次数

全局 shell 变量名

我需要扩大一些外壳(未环境)的变量名是主题相关的,例如B2_...在那里...也能像一个或多个不同的事情ACCOUNT_IDACCOUNT_KEYRESPOSITORY等等。

我不知道有多少变量,也不知道它们是哪些。这就是我想知道的。

我希望能够遍历B2...变量而不必将每个单独的名称放在列表中,类似于我将 glob 文件名扩展的方式。

我将 zsh 用于交互式会话,但 sh 或 bash 的解决方案也很好。

shell variable

7
推荐指数
3
解决办法
769
查看次数

在变量名中调用其他变量(Bash)

目前我有下一个路径

langs="EN GE"
dir_EN=/xx
dir_GE=/zz
Run Code Online (Sandbox Code Playgroud)

如您所见,变量 $langs 在单个数组中包含所有可能的语言。我想使用检查语言是什么的循环将所有这些路径保存在多语言(dir_ML)数组中,然后保存相应的路径。这是我到目前为止所拥有的

for i in $(seq 0 1); do #I used seq because I didn't know how to specify the length of the variable $langs
    dir_ML[$i]=dir_${langs[$i]}
done
Run Code Online (Sandbox Code Playgroud)

我正在寻找的输出是

dir_ML[0]=/xx

dir_ML[1]=/zz

我希望你能理解我在做什么!提前致谢

bash shell-script variable-substitution variable

7
推荐指数
2
解决办法
2071
查看次数

我的变量的值为 0,但我没有将其设置为 0

今天我注意到我的一个 zsh 函数不起作用;我调查了这个问题,罪魁祸首是:

for i in a b
do
echo "$i"
done
0
0
Run Code Online (Sandbox Code Playgroud)

然后我打开了一个新的 zsh,一切正常:

for i in a b
do
echo "$i"
done
a
b
Run Code Online (Sandbox Code Playgroud)

任何人都可以解释第一个对我来说很奇怪的原因吗?

zsh variable

7
推荐指数
1
解决办法
337
查看次数