【问题标题】:BASH ,"touch" and "ln-s" inside for loopBASH "touch" 和 "ln-s" 在 for 循环中
【发布时间】:2020-11-01 10:51:25
【问题描述】:

运行此代码时,我收到此错误:

意外标记附近的语法错误(' touch FILE$i FILE($i+1);'

怎么了?

#!/bin/bash

  for ((i=1; i<=99; i++ ));
    do        
    touch FILE$i FILE($i+1);
    ln -s FILE$i FILE($i+1);
       
     done


【问题讨论】:

  • 题外话:当你要去ln -s FILE 1 FILE2时,你不需要先触摸FILE2。您的代码正在尝试创建一系列符号链接。你确定你想要那个,还是想要 FILE2,..,FILE99 都链接到 FILE1。在这种情况下,您可能需要touch FILE1; for ((i=2;i&lt;=99;i++));do ln -s FILE$i; done

标签: bash for-loop syntax touch


【解决方案1】:

要进行算术运算,请使用$((...)) 语法:

    touch FILE$i FILE$(($i+1))

https://www.gnu.org/software/bash/manual/bash.html#Arithmetic-Expansion

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-07
    • 2013-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-05
    相关资源
    最近更新 更多