简体   繁体   English

在Unix Shell脚本中嵌套for循环

[英]Nested for loop in unix shell script

I am creating the shell script that will take two condition first it will get all file name in and insert one by one in html field other for loop will get the count and it will used for auto increment in serial number. 我正在创建将首先满足两个条件的shell脚本,它将获取所有文件名,并在html字段中一个接一个地插入其他for循环将获取计数,并将其用于序列号的自动递增。

I have written code like this 我写了这样的代码

cnt=`find /optware/oracle/logs/20190311_JAVA/TEMP/  -type f | wc -l`
    for ((i in `ls -l /optware/oracle/logs/20190311_JAVA/TEMP/|grep -v ^$|awk '{print $9}` && a=1; a <= $cnt ; a++)) 
    do
    echo "  <tr> $a <td></td><td>$i</td></tr>" >>Temp.lst
    done

I am getting error as && a=1: arithmetic syntax error 我收到错误,因为&& a = 1:算术语法错误

Kindly help me with this issue. 请帮我解决这个问题。

The ((...)) should contain three arithmetic expressions. ((...))应该包含三个算术表达式。 in is not an arithmetic operator. in不是算术运算符。 In fact, it's unclear what you're trying to achieve. 实际上,目前尚不清楚您要实现什么目标。

To nest two loops, specify each one with its own for : 窝两个循环,指定每个都拥有自己for

for i in `ls -l /optware/oracle/logs/20190311_JAVA/TEMP/|grep -v ^$|awk '{print $9}` ; do
    for (( a = 1 ; a <= cnt ; a++ )) ; do
        echo "$i:$a"
    done
done

If you just need a counter, there's no need for the second loop: 如果只需要一个计数器,则不需要第二个循环:

a=1;
for i in `ls -l /optware/oracle/logs/20190311_JAVA/TEMP/|grep -v ^$|awk '{print $9}` ; do
    echo "$i:$a"
    (( ++a ))
done

You can't loop over an index+value pair directly in Bash. 您不能直接在Bash中循环使用“索引+值”对。 I would write this code as follows: 我将编写以下代码:

index=0
for path in /optware/oracle/logs/20190311_JAVA/TEMP/*
do
    echo "  <tr>${index}<td></td><td>${path}</td></tr>" >> Temp.lst
    ((++index))
done

You're mixing up two looping constructs . 您正在混淆两个循环结构 You can use either one, but not both together. 您可以使用任何一个,但不能同时使用。

for name [ [in [words …] ] ; ] do commands; done

and

for (( expr1 ; expr2 ; expr3 )) ; do commands ; done

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM