• 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

  • 相关阅读:
    CC2540-BLE4.0 学历笔记1之串口体验
    cc2530的PWM实现
    用qt写的一个简单到不能在简单的上位机
    在Kubuntu14.04中安装小企鹅输入法
    初始化umi项目
    工具杂记(三) -- zookeeper
    工具杂记(二) -- Centos7
    工具杂记(一) -- vagrant
    (一)Docker导学
    JVM随笔(二)之搜索类
  • 原文地址:https://www.cnblogs.com/qjfoidnh/p/13186971.html
Copyright © 2020-2023  润新知