简体   繁体   English

变量引用在 shell 脚本中的 echo 命令中不起作用

[英]Variable reference not working in echo command in shell script

I created two files with exact same data 'a','b','c','d','e' in separate rows我在不同的行中创建了两个具有完全相同数据'a'、'b'、'c'、'd'、'e'的文件
1. testing_office.csv file from ms office 2019. 1. 来自 ms office 2019 的 testing_office.csv 文件。
2. testing_txt_editor.csv using sublime text 2. testing_txt_editor.csv 使用崇高文本
Following is shell script which is used.以下是使用的 shell 脚本。

object_type=$1

for object in `cat ${object_type}`
do
echo "test before object reference" ${object}
echo ${object} "random text after the object reference"
done

i am running the code via zsh shell Following are the commands used and outputs我正在通过 zsh shell 运行代码以下是使用的命令和输出

argarvit >>% ./testing.zsh testing_office.csv 
test before object reference a
a random text after the object reference
test before object reference 
 random text after the object reference
test before object reference b
 random text after the object reference
test before object reference c
 random text after the object reference
test before object reference d
 random text after the object reference
test before object reference e
e random text after the object reference

argarvit >>% ./testing.zsh testing_txt_editor.csv 
test before object reference a
a random text after the object reference
test before object reference b
b random text after the object reference
test before object reference c
c random text after the object reference
test before object reference d
d random text after the object reference
test before object reference e
e random text after the object reference

Why are the two files acting different if there is some text after the reference?如果引用之后有一些文本,为什么这两个文件的行为会有所不同? The issue might be with encoding but how do i figure out the exact reason?问题可能与编码有关,但我如何找出确切的原因? I tried opening testing_office.csv on text editor it looks the same.我尝试在文本编辑器上打开 testing_office.csv 它看起来一样。 I tried on MBP as well as AL2 getting same results on both the terminals.我尝试在 MBP 和 AL2 上在两个终端上获得相同的结果。

You are using a Useless Use of cat , alas I cannot recreate your problem.您正在使用cat 的无用用法,唉,我无法重现您的问题。 Likely there is a core difference between the files stated.所述文件之间可能存在核心差异。 You can od -c each file to check.您可以od -c每个文件进行检查。

You may want to try a while read loop instead of backtick expansion:您可能想尝试一个while read循环而不是反引号扩展:

while read object ; do
        echo "test before object reference" ${object}
        echo ${object} "random text after the object reference"
done <$1

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

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