[英]Setting numeric variables in a script (“set $i 1”) not working (“echo $i” empty)
As the title says I need help with a bash script that has to generate a string given two numeric variables. 如标题所述,我需要有关bash脚本的帮助,该脚本必须生成给定两个数字变量的字符串。 That string will be used to generate a file name, but when I test the name generation code it yields nothing
该字符串将用于生成文件名,但是当我测试名称生成代码时,它不会产生任何结果
The script is the code that follows 脚本是下面的代码
# !usr/bin
set nombre
declare -a i
declare -a j
set $i 1
set $j 2
set nombre "$i\_$j.txt"
echo $i
echo $j
Here is what it yields: 它产生的是:
entropy@3PY:~$ ./test4
As you can see it yields nothing while it should yield 如您所见,它应该不会产生任何收益
1
2
thanks in advance 提前致谢
set
is not used to assign values to regular variables; set
不用于将值分配给常规变量; it is used to set the values of the positional parameters or to modify shell options. 它用于设置位置参数的值或修改壳选项。 You need a regular assignment.
您需要定期分配。
i=1
j=2
nombre="${i}_$j.txt"
echo "$i"
echo "$j"
echo "$nombre"
There is no need to declare variables prior to assignment; 分配前无需声明变量; assignment creates a variable if necessary.
必要时,分配会创建一个变量。 The
declare
command is more about setting attributes on a name ( -a
, for instance, would mark the variable as an array variable, something you do not need here). 该
declare
命令更多的是关于一个名字设置属性 ( -a
,例如,将标志着变量作为数组变量,有些东西你不需要在这里)。
As an example of how set
does work, consider 作为如何一个例子
set
确实可行,考虑
echo "First positional parameter: $1"
echo "Second positional parameter: $2"
set foo bar # $1=foo, $2=bar
echo "First positional parameter: $1"
echo "Second positional parameter: $2"
Does this code works as desired? 此代码是否可以按需工作?
#!/bin/bash
i=1
j=2
echo $i
echo $j
nombre=$i"_"$j.txt
echo $nombre
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.