zoukankan      html  css  js  c++  java
  • Ubuntu中执行bash脚本出现"Global symbol "$xxx" requires explicit package name at (eval 1) line 1."

    问题描述

    在Ubuntu14里编写了一个很简单的文件批量重命名脚本

    #!/bin/bash
    read -p "请输入50递增的起始数字:" startA
    echo "
    "
    read -p "请输入1递增的起始数字:" startB
    echo "
    "
    read -p "请输入1递增的结束数字:" endB
    while [ $startB -le $endB ]
    do
        rename 's/traceroute-1000-$startA/traceroute-1000-$startB/' *
        let startA+=50
        let startB+=1
    done

    但运行后却报了之前从没见过的错误:

    Global symbol "$startA" requires explicit package name at (eval 1) line 1.
    Global symbol "$startB" requires explicit package name at (eval 1) line 1.

    在查询后,发现大多数答案提到此错误都是在Perl脚本里;而且此后经过一些修改,还出现过

    Bareword "Build" not allowed while "strict subs" in use at (eval 1) line 1.

    这样的报错信息。

    解决方案

    在某国外论坛上找到了原因

    It's a Perl error.

    (In Ubuntu) the rename command is a Perl script and in the terminal emulator you are using Bash. The single quotes you used in your command are preventing Bash to perform the parameter expansion. Please see: http://mywiki.wooledge.org/Quotes

    总的来说,rename的实现是一个Perl脚本,而Perl的语法与Bash有些不同,将rename句改为如下即可

    rename "s/traceroute-1500-${startA}/traceroute-1500-${startB}/" *

    It's a Perl error.

    (In Ubuntu) the rename command is a Perl script and in the terminal emulator you are using Bash. The single quotes you used in your command are preventing Bash to perform the parameter expansion. Please see: http://mywiki.wooledge.org/Quotes

  • 相关阅读:
    LeetCode1049. 最后一块石头的重量 II
    LeetCode416. 分割等和子集
    LeetCode96. 不同的二叉搜索树
    LeetCode343. 整数拆分
    python笔记---内置容器
    Numpy学习笔记(一)
    tensorflow入门代码分析
    神经网络
    回归算法
    机器学习入门笔记
  • 原文地址:https://www.cnblogs.com/qjfoidnh/p/13186971.html
Copyright © 2011-2022 走看看