Shell脚本不必要的“?”符的文件名末尾 [英] Shell Scripting unwanted '?' character at the end of file name

查看:142
本文介绍了Shell脚本不必要的“?”符的文件名末尾的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到一个多余的?在我的文件名末尾,而这样做:

I get an unwanted '?' at the end of my file name while doing this:

emplid=$(grep -a "Student ID" "$i".txt  | sed 's/(Student ID:  //g' | sed 's/)Tj//g' ) 
 #gets emplid by doing a grep from some text file
echo "$emplid"   #prints employee id correctly 
cp "$i" "$emplid".pdf  #getting an extra '?' character after emplid and before .pdf

即不但得不到像123456.pdf文件名,我得到123456?.PDF。
这究竟是为什么,如果回声正确打印?
我怎样才能删除尾随问号字符?

i.e instead of getting the file name like 123456.pdf , I get 123456?.pdf . Why is this happening if the echo prints correctly? How can I remove trailing question mark characters ?

推荐答案

这听起来像你的脚本文件具有DOS风格的行结束符(\\ r \\ n)的,而不是UNIX风格(只是\\ n)的 - 当一个脚本在此格式中,\\ r被视为命令的一部分。在这种情况下,它变得纳入$ EMPLID因此在文件名

It sounds like your script file has DOS-style line endings (\r\n) instead of unix-style (just \n) -- when a script in this format, the \r gets treated as part of the commands. In this instance, it's getting included in $emplid and therefore in the filename.

许多平台支持 DOS2UNIX的命令将文件为UNIX风格的行结束转换。一旦它的转换,坚持文本支持Unix风格的文本文件编辑器。

Many platforms support the dos2unix command to convert the file to unix-style line endings. And once it's converted, stick to text editors that support unix-style text files.

编辑:我曾以为这个问题行尾分别在shell脚本,但看起来他们是在输入文件($ I.TXT)代替。您可以使用DOS2UNIX的输入文件进行清洁和/或在脚本中添加清洁步骤sed命令。顺便说一句,你可以有sed的使用-e选项应用多个编辑一个实例:

I had assumed the problem line endings were in the shell script, but it looks like they're in the input file ("$i".txt) instead. You can use dos2unix on the input file to clean it and/or add a cleaning step to the sed command in your script. BTW, you can have a single instance of sed apply several edits with the -e option:

emplid=$(grep -a "Student ID" "$i".txt  | sed '-e s/(Student ID:  //g' -e 's/)Tj//g' -e $'s/\r$//' )

我推荐的的使用 SED的/.$//' - 如果文件是UNIX格式,即'会切断文件名的最后一个字符。

I'd recommend against using sed 's/.$//' -- if the file is in unix format, that'll cut off the last character of the filename.

这篇关于Shell脚本不必要的“?”符的文件名末尾的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆