简体   繁体   English

bash脚本中的变量损坏

[英]Variable Corruption in bash script

Given the following code I am expecting the variable $NewFile to be /var/ftp/pub/faxes/myfile.txt.pdf 给定以下代码,我期望变量$ NewFile为/var/ftp/pub/faxes/myfile.txt.pdf
However my system is returning: ".pdf/ftp/pub/faxes/myfile.txt" 但是我的系统返回:“ .pdf / ftp / pub / faxes / myfile.txt”

$Ext returns: "txt" $ Ext返回:“ txt”
$oFileName returns: "/var/ftp/pub/faxes/myfile.txt" $ oFileName返回:“ /var/ftp/pub/faxes/myfile.txt”

I have tried this a hundred different ways with no luck. 我没有运气就尝试了一百种不同的方法。

PDF=".pdf"

#extract the file.ext from the script
FileName1=`head $f -n1 | awk -F/ '{print $NF}' | sed 's/\"//'`
FileName2=`head $f -n1 | awk -F/ '{print $NF}' | sed 's/\"//'`
FileName3=`head $f -n1 | awk -F/ '{print $NF}' | sed 's/\"//'`

FileName=`basename $FileName1`
Ext=`echo $FileName | awk -F . '{print $NF}'`

FileName=`basename $FileName2`
oFileName=/var/ftp/pub/faxes/${FileName}

FileName=`basename $FileName3`
NewFile=/var/ftp/pub/faxes/${FileName}${PDF}

echo $oFileName
echo $NewFile
echo $Ext

Any help is appreciated. 任何帮助表示赞赏。

Comment: if you ever move to a normal Unix-like system instead of Linux, you won't get away with ' head $f -n1 '; 注释:如果您转而使用常规的类Unix系统而不是Linux,那么' head $f -n1 '是无法避免的。 you must put option (control) arguments before file names, so use ' head -n1 $f '. 您必须将选项(控制)参数放在文件名之前,因此请使用' head -n1 $f '。 (If you set environment variable POSIXLY_CORRECT, Linux will behave the same as other systems.) (如果设置环境变量POSIXLY_CORRECT,Linux的行为将与其他系统相同。)

Comment: you could perfectly well write: 评论:您完全可以写:

FileName1=`head -n1 $f | awk -F/ '{print $NF}' | sed 's/\"//'`
FileName2=$Filename1
FileName3=$Filename1

It makes more sense than running the same three programs on the same data three times. 这比在相同数据上运行相同的三个程序三次要有意义。

Comment: you should learn and use the '$(...)' notation: 评论:您应该学习并使用'$(...)'表示法:

FileName1=$(head -n1 $f | awk -F/ '{print $NF}' | sed 's/\"//')

When run on MacOS X, with f=xxxx.input and the file xxxx.input containing one line that says '/some/where/myfile.txt', the script produces: 在MacOS X上运行时,如果f = xxxx.input和文件xxxx.input包含一行表示“ /some/where/myfile.txt”的脚本,则脚本将生成:

/var/ftp/pub/faxes/myfile.txt
/var/ftp/pub/faxes/myfile.txt.pdf
txt

The trace output ('bash -x') is: 跟踪输出(“ bash -x”)为:

+ f=xxxx.input
+ PDF=.pdf
++ head -n 1 xxxx.input
++ sed 's/\"//'
++ awk -F/ '{print $NF}'
+ FileName1=myfile.txt
++ head -n 1 xxxx.input
++ awk -F/ '{print $NF}'
++ sed 's/\"//'
+ FileName2=myfile.txt
++ awk -F/ '{print $NF}'
++ head -n 1 xxxx.input
++ sed 's/\"//'
+ FileName3=myfile.txt
++ basename myfile.txt
+ FileName=myfile.txt
++ echo myfile.txt
++ awk -F . '{print $NF}'
+ Ext=txt
++ basename myfile.txt
+ FileName=myfile.txt
+ oFileName=/var/ftp/pub/faxes/myfile.txt
++ basename myfile.txt
+ FileName=myfile.txt
+ NewFile=/var/ftp/pub/faxes/myfile.txt.pdf
+ echo /var/ftp/pub/faxes/myfile.txt
/var/ftp/pub/faxes/myfile.txt
+ echo /var/ftp/pub/faxes/myfile.txt.pdf
/var/ftp/pub/faxes/myfile.txt.pdf
+ echo txt
txt

You will need to show what you're really using, and 'bash -x' may well help you see where your problem is -- I cannot reproduce it. 您将需要显示您真正在使用什么,'bash -x'可能很好地帮助您了解问题所在-我无法重现。

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

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