Ubuntu中执行bash脚本出现"Global symbol "$xxx" requires explicit package name at (eval 1) line 1."

问题描述

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

#!/bin/bash
read -p "请输入50递增的起始数字:" startA
echo "\n"
read -p "请输入1递增的起始数字:" startB
echo "\n"
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

posted @ 2020-06-24 12:31  qjfoidnh  阅读(738)  评论(1编辑  收藏  举报