-
2021-04-26 10:03:25
- shell中使用管道会生成一个子shell,在子shell中使用while、for循环的代码也是在子shell中执行的,所以在循环中的修改的变量只在子shell中有效,当循环结束时,会回到主shell,子shell中修改的变量不会影响主shell中的变量
代码如下:
A="1" B="2" C="/home/linux/a" cat $C | grep -v '^commit' | while read line do if [ "x$A" = "x1" ]; then B=$A echo $B fi done echo $B 第一个echo打印的是1 第二个echo打印的是2
这里是因为在子shell中的while循环中的B只是主shell中B的一个副本,在子shell中对B重新赋值是不能影响到父shell的,所以最后echo $B时值没有改变。
以下是可以的:
while read line do if B=$A fi done < $C
这样是可以重新赋值的,因为这里没有管道,也就不存在子shell了
更多相关内容 - shell中使用管道会生成一个子shell,在子shell中使用while、for循环的代码也是在子shell中执行的,所以在循环中的修改的变量只在子shell中有效,当循环结束时,会回到主shell,子shell中修改的变量不会影响主shell中的变量
-
浅谈shell循环中变量的作用域问题
2020-09-15 03:38:43今天小编就为大家分享一篇浅谈shell循环中变量的作用域问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧 -
shell while循环后变量的值未变化
2019-08-26 16:25:40工作中想遍历文件中的每行,并且赋值给一个变量,使用下面写法,但是循环遍历后变量依然为空,值没有变化。如下: ~/temp/201908/temp/temp$ cat temp.txt http://www.baidu.com http://www.jd.com ...工作中想遍历文件中的每行,并且赋值给一个变量,使用下面写法,但是循环遍历后变量依然为空,值没有变化。如下:
~/temp/201908/temp/temp$ cat temp.txt http://www.baidu.com http://www.jd.com http://www.huawei.com ~/temp/201908/temp/temp$ cat temp.sh #! /bin/bash file_path=temp.txt new_var='' cat ${file_path} | while read line do new_var="${new_var}${line};" echo ${line}_____ done echo "${new_var}+++++" ~/temp/201908/temp/temp$ source temp.sh http://www.baidu.com_____ http://www.jd.com_____ http://www.huawei.com_____ +++++
上面未赋值成功是因为使用了管道符,将值传给了while,使得while在子shell中执行,子shell中的变量等在循环外无效。
可以写为:
~/temp/201908/temp/temp$ cat temp.sh #! /bin/bash file_path=temp.txt new_var='' while read line do new_var="${new_var}${line};" echo ${line}_____ done <<< "$(cat ${file_path})" echo "${new_var}+++++" ~/temp/201908/temp/temp$ source temp.sh http://www.baidu.com_____ http://www.jd.com_____ http://www.huawei.com_____ http://www.baidu.com;http://www.jd.com;http://www.huawei.com;+++++
或者:
~/temp/201908/temp/temp$ cat temp.sh #! /bin/bash file_path=temp.txt new_var='' while read line do new_var="${new_var}${line};" echo ${line}_____ done < ${file_path} echo "${new_var}+++++" ~/temp/201908/temp/temp$ source temp.sh http://www.baidu.com_____ http://www.jd.com_____ http://www.huawei.com_____ http://www.baidu.com;http://www.jd.com;http://www.huawei.com;+++++
-
自学shell编程——第3讲(for语句/while语句/until语句)/在while循环内设置的shell变量在其外部不可见
2022-04-16 10:18:28自学shell编程——第3讲(for语句/while语句/until语句) 三种循环语句:for语句/while语句/until语句。 1. for循环语句:for 循环变量 in 次数-do-循环体-done 以一个例子作为练习,熟悉for循环语句。这里比较难的...自学shell编程——第3讲(for语句/while语句/until语句)
三种循环语句:for语句/while语句/until语句。
一个重要问题:在while循环内设置的shell变量在其外部不可见(巨重要,我最近就犯错了)
这个可能与shell相关,有时我会遇到,有时没有,
解决方法:用花括号{},将while循环和后面的内容括起来。cat ${CleandataFile} | { while read line do array=($line) echo ${array[0]} all_data=$all_data" "${array[0]} done echo $all_data } #如果没有这个花括号,echo是没有内容的。
1. for循环语句:for 循环变量 in 次数-do-循环体-done
以一个例子作为练习,熟悉for循环语句。这里比较难的是次数(就是循环限制)的书写
#在定义变量时定义属性 declare -i num=2 #将变量nun直接定义为整数型,值为2 num=$num*2;echo $num #输出4;若是前面直接为num=2,则输出2*2 #计算2^10的结果1024,命令行输入2和10 result=1 #当然这里你可以使用declare -i result=1,那么下面的result=$result*$1 for num in `seq 1 $2` #循环变量为num,每次循环+1 do result=`expr $result \* $1` done echo $result #执行:./for.sh 2 10;输出1024 #计算1+2+3+……+100的结果5050,命令行输入1和100 declare -i result=0 for num in `seq $1 $2` do result=$result+$num done echo $result #执行:./for.sh 1 100;输出5050。执行:./for.sh 2 10;输出54 #将命令行参数依次输出,这里是对$*进行遍历 declare -i n=1 for argu in $* do echo "argument $n is $argu." n=$n+1 done #执行:./for.sh 1 100;输出argument 1 is 1. argument 2 is 100. #对数组内容进行遍历 name="zhao qian sun li" declare -i n=1 for argu in $name do echo "argument $n is $argu." n=$n+1 done #执行:./for.sh;输出argument 1 is zhao. argument 2 is qian. argument 3 is sun. argument 4 is li.
简单小结:for可以执行一定次数的循环,也可以对数组进行遍历。多多联系,习惯书写,还是有很多小坑需要自己踩一踩
2. while循环语句:while 判断条件-do 循环体-done
以一个例子作为练习,熟悉while循环语句。判断条件的书写:使用test测试语句或者[],循环体内书写如何循环。
#!/bin/bash #计算两个数之间所有偶数的和。(2-10:2+4+6+8+10;3-10:4+6+8+) declare -i i declare -i sum=0 #书写习惯,在使用变量前先定义变量。i为循环变量,sum为累积 #这里对输入的两个数进行判断,第一个数,若为偶数,则保留;否则加1。第二个数,若为偶数,则保留;否则减1 if [ `expr $1 \% 2` -eq 0 ] then i=$1; else i=`expr $1 + 1` fi if [ `expr $2 \% 2` -eq 0 ] then end=$2; else end=`expr $2 - 1` fi while [ $i -le $end ] #-le表小于等于 do sum=$sum+$i i=$i+2 #每次加2 done echo $sum #执行:./while.sh 2 10,输出30;./while.sh 3 9;输出18;./while.sh 3 10;输出28 #计算10!,输出3,628,800 declare -i i=1 declare -i result=1 #书写习惯,在使用变量前先定义变量。i为循环变量,sum为累积 while test $i -le $2 do result=$result*$i i=$i+1 done echo $result #执行:./while.sh 2 10,输出3,628,800;
这里加一个小小的需要思考的题目。(这里你先思考一下,再看给出的答案,输出结果即可,不一定和我的相同)键盘输入3,输出图形
;输入4,输出图形
#!/bin/bash if test $# -ne 1 #判断输入一个参数 then echo "input one arguments." fi #这里将这个图形看成一个矩阵 declare in i=1 declare in j=1 end=`expr $1 \* 2 - 1` #变量end为矩阵大小 for i in ` seq 1 $end ` do str="" #变量str存储每一行的字符串 if [ $i -le $1 ] #先输出前半部分 then for j in `seq 1 $end` do left=`expr $1 - $i`;rigth=`expr $1 + $i` if [[ $j -le $left ]] || [[ $j -ge $right ]] #使用逻辑或 then str=$str" " else str=$str"*" fi done else #输出下半部分 for j in `seq 1 $end` do left=`expr $i - $1`;right=`expr 3 \* $1 - $i` if [[ $j -le $left ]] || [[ $j -ge $right ]] then str=$str" " else str=$str"*" fi done fi echo "$str" done #执行:./image.sh 4
脚本执行结果如下:
知识点:1. 输出的变量中存在连续空格时,使用echo “$str”;而不是echo $str。后者输出只保留一个空格。2.使用逻辑关系或与非时,使用[[判断条件]]。
3. until循环语句:until 判断条件-do 循环体-done。因为until循环和while循环语句不同在于判断条件刚好相反。容易理解,但是又容易混,所以我自己就是只使用while循环。尽量不使用until,除非题目要求或者特殊情况下。当然一些大佬,为了显示自己擅长写shell,用until,但是和while一样。
这里只是简单的一个小例子。在终端输出1~10
#!/bin/bash declare -i i=1 until test $i -gt 10 do echo $i i=$i+1 done
总结:循环语句常用for和while。且会与条件语句混合使用,多多练习。
-
【Shell】while 循环中的变量无法保存|无法获取while中的变量|管道中的函数变量无法获取问题
2021-09-02 22:43:52echo "adasd" | while read line do x=2 done echo $x 运行结果是 [liuhao@slave04 ~]$ sh test.sh 1 原因 原来是因为管道|创建了新的子进程,而子进程是在独立的进程空间(Context)运行了. 需要跟父进程通信...例子:
[liuhao@slave04 ~]$ cat test.sh #! /bin/sh x=1 echo "adasd" | while read line do x=2 done echo $x 运行结果是 [liuhao@slave04 ~]$ sh test.sh 1
原因
原来是因为管道|创建了新的子进程,而子进程是在独立的进程空间(Context)运行了. 需要跟父进程通信的话, 得使用进程间通信机制. 不是简单的变量问题。
解决办法:
1、命名管道
shell中引用while中的变量 - Shell-Chinaunix
mkfifo pipe; exec 3<>pipe; #fd3 指向pipe echo "a b c" |while read line1 line2 do echo $line1 >&3 # 写入fd3 done read -u3 var #读取变量 echo $var rm pipe; exec 3>&-
2、改为管道输入为文件输入
shell while内获取外部变量内容 - 李秋 - 博客园
#!/bin/sh x="this is the initial value of x" while read line;do x="$line" echo $x done < /tmp/tmp echo x = $x
3、输入重定向
shell while内获取外部变量内容 - 李秋 - 博客园
#!/bin/sh x="this is the initial value of x" exec 3<&0 # save stdin 将标准输入重定向到文件描述符3 exec < /tmp/tmp # 输入文件 while read line; do x=$line echo $x done exec 0<&3 # restore stdin echo x = $x
4、临时文件桥接
MY_UUID=$(uuidgen)#或者$(cat /proc/sys/kernel/random/uuid) MY_TMP_FILE_PATH=/tmp/${MY_UUID}.txt x="this is the initial value of x" while read line do x=$line echo $x done < ${MY_TMP_FILE_PATH} echo $x
-
while读取文件 Shell中while循环的陷阱, 变量实效, 无法赋值变量
2020-12-10 19:31:20在写while循环的时候,发现了一个问题,在while循环内部对变量赋值、定义变量、数组定义等等环境,在循环外面失效。 一个简单的测试脚本如下: #!/bin/bash echo "abc xyz" | while read line do new_var=$line ... -
shell 关于 while 循环中赋值的问题
2021-10-03 08:00:34shell 关于 while 循环中赋值的问题 程序1 #!/bin/sh count=0 cat temp.txt|while read fre do count=$(($count+$fre)) done echo "$count" exit 1 程序2 #!/bin/sh count=0 exec 3< temp.txt while -
shell循环中变量的作用域问题shell变量失效
2022-01-11 15:36:04最近实现了一个shell脚本,功能简单来说就是从文件中按行读取然后将所有行拼接成一行写入一个文件,关键代码如下 path_all="" cat $1 | while read line ...按照常理shell中的变量默认是全局变量,不会存在变量 -
Linux shell - while 循环对外部变量的处理
2019-10-01 17:07:11用过linux shell里面的while循环的都知道,循环里面对外部变量的修改是不生效的。比如: variable = old_value cat file | while read line do do something variable = new_value done echo $varable 输出将... -
解决:shell中使用while read line循环,变量运算不生效的问题
2022-02-15 11:22:29解决:shell中使用while read line循环,变量运算不生效的问题 -
while 循环内的变量
2018-11-27 11:21:44通过管道传递参数给while循环后,while循环会通过子shell的循环的方式执行,故循环内对变量做的操作,在当前shell内无法生效。 #sleep也是通过子shell进行,故如果sleep的进程意外终止,则shell执行的效果可能与... -
SHELL的for循环和while循环
2022-04-15 11:47:05文章目录前言一、for循环二、使用步骤1.引入库2.读入数据总结 前言 提示:这里可以添加本文要记录的大概内容: 例如:随着人工智能的不断发展,机器学习这门技术也越来越重要,很多人都开启了学习机器学习,本文就... -
shell循环体内变量传递无效
2021-05-27 21:53:32#!...虽然执行了RetVal=1这条语句,但是一出while循环,变量值又变成0了。 这是因为管道是在子shell中执行的,子shell中的变量对于子shell之外的代码块来说, 是不可见的. 当然, 父进程也不能访问 这 -
Shell脚本中的while循环
2020-07-19 10:54:52Today we’ll learn about the while loop in shell scripts. Loops are an essential part of any programming language. When we write a code to execute a set of statements 15 times, writing the same ... -
shell中循环for与while和如何跳出循环以及函数的使用
2022-07-25 17:38:39shell中循环for与while和如何跳出循环的使用 -
shell脚本(三)while循环语句
2021-05-11 19:23:13while循环语句的运行过程使用while循环语句时,可以根据特定的条件反复执行一个命令,直到条件不满足为止;while会出现死循环的过程,因此循环体内的命令序列内应包括修改测试条件的语句while语句的语法结构:注意:... -
shell脚本-- while循环中的重定向
2019-09-06 17:23:35一 read命令 read [-ers] [-a aname] [-d delim] [-i text] ...-a 后跟一个变量,该变量会被认为是个数组,然后给其赋值,默认是以空格为分割符。 -d 后面跟一个标志符,其实只有其后的第一个字符有用,作为结束的... -
shell脚本中,whlie循环中的赋值没法传递到循环外
2022-04-22 10:04:09while read line do #echo $line #echo $j imyarray[$j]="$line" echo ${imyarray[*]} let j++ done < $tables echo ${imyarray[*]} } mesh_sys_config mesh_sys_config mesh_device_upgrade_log mesh_sys... -
linux shell循环:for、while、until用法详解
2020-09-15 00:41:49主要介绍了linux shell下常用的循环for、while、until的用法,这也是脚本之家小编看到的比较详细的文章了,感兴趣的朋友可以参考一下,最好是在环境下自己手工打一份,不要复制 -
【Shell】Shell脚本(for循环,while循环,break跳出循环,continue结束本次循环)
2021-04-07 14:00:24for循环 语法:for 变量名 in 条件 ; do done; 案例一: 计算1-100所有数字的和。 脚本: #!/bin/bash sum=0 for i in `seq 1 100` do sum=$[$sum+$i] done echo $sum 结果: [root@congji ~]# sh... -
shell 管道循环内对外部变量赋值失败
2021-04-13 11:55:23问题 写shell的时候遇到一段未如预期执行的代码: ... str="" mysql -u xx -h localhost -pxx -Ne "select col1,col2 from db1.tbl1" |...这是因为使用了管道|,在管道中的循环逻辑不能作用到外部变量,也就是说在上述do -
22.shell语言之for循环、while循环、until循环、exit、break、continue语句
2022-04-14 09:13:38这组值可以是任意字符串的集合(shell 在默认情况下所有变量都是以字符串的形式存储的),它们可以在程序里被列出,更常见的做法是使用 shell 的文件名扩展结果。 for 循环将会重复整个对象列表,依次执行每一个独立... -
Shell编程中循环语句(for循环、while循环、until循环)
2022-04-10 23:04:391、for循环 for循环:读取不同的变量值,用来逐个执行同一组命令,即for循环时遍历的过程 格式: for 变量名 in 取值列表 do 命令序列 done -
shell脚本变量在循环中有值,出了循环就为空的问题解决
2020-06-05 14:36:30最近遇到一个问题,在循环之前定义了一个变量,循环中会给该变量赋值,可是出了循环后该变量就变成空串了。在java中是不会出现这种情况的,百思不得其解,最后终于解决了,所以很有必要记下来,避免以后出现同样的...
收藏数
41,235
精华内容
16,494