传递通配符以在Shell脚本中剪切命令并将其存储在变量中 [英] pass wildcard to cut command in shell script and store it in a variable

查看:101
本文介绍了传递通配符以在Shell脚本中剪切命令并将其存储在变量中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Shell的新手,我遇到一种情况,我试图评估特定列的唯一值,以检查它们在Shell脚本中是否有效,该脚本随后将被调用.

I am new to shell, I have a case where I am trying to evaluate a particular column unique values to check if they are valid in a shell script which will be invoked later.

根据我的搜索,我认为它与sort&做到这一点很独特

From my searches I think cut along with sort & unique is good to do it

所以我的尝试是

file=/filepath/*vendor.csv
file_categories = `cut -d, -f1 $file |sort |unique`

$ file应该包含文件名中包含供应商的文件

$file should hold file which has vendor in its filename

但是即使使用命令替换(`)之后,$ file也不会被正确的文件名替换,它只会放置文件中的内容

but even after using command substitution (`) the $file is not getting replaced with the correct filename , it just places what is present in file

我正在尝试的另一个示例是

Another example for what I am attempting is

a=/stage/scripts/vendor/*.out
echo $a
/stage/Scripts/ecommerce/oneclick/nohup.out /stage/Scripts/ecommerce/Vendor/Vendor_Automate_Ingestion_Process.out

wc-l

wc: /stage/Scripts/ecommerce/vendor/*.out:

$(wc -l "$a")
wc: /stage/Scripts/ecommerce/vendor/*.out:No such file or directory

我想了解如何在命令替换中传递通配符,以及如何纠正.

I want to understand how we can pass wild characters in command substitution and what I can do to rectify.

推荐答案

否,file将包含带通配符的文字字符串.当您对值$file进行插值时,如果不带引号,则shell将其评估为通配符. echo "$file"用正确的引号显示给您变量的实际值.

No, file will contain the literal string with the wildcard. When you interpolate the value $file without quotes around it, that's when the shell evaluates it as a wildcard. echo "$file" with proper quoting shows you the actual value of the variable.

没有一种很好的方法将文件名列表存储在常规shell变量中. Ksh和其他一些shell具有用于此目的的数组,但是它不能移植回通用sh,并且可能不是您实际需要的东西,这取决于您要实现的最终目标.如果要从与通配符匹配的文件中的字段中提取唯一值成字符串,只需确保分配中等号周围没有空格就可以了.

There is no good way to store a list of file names in a regular shell variable. Ksh and some other shells have arrays for this purpose, but it's not portable back to generic sh and may be something else than what you actually need, depending on what end goal you are trying to accomplish. If you want to extract unique values from a field in the files matching the wildcard into a string, just make sure you don't have spaces around the equals sign in the assignment and you're done.

file_categories=$(cut -d, -f1 $file | sort -u)
#              ^ no spaces around the equals sign!

在这里将通配符存储在变量中是可疑的;如果这是您要解决的问题,可能直接使用通配符即可.

Storing the wildcard in a variable is dubious here; probably simply use the wildcard directly if this is the problem you want to solve.

在每个不特别要求外壳扩展通配符并标记字符串的地方,都需要在其周围加上双引号.

Everywhere you don't specifically want the shell to expand wildcards and tokenize a string, you need to put double quotes around it.

echo "$file_categories"

该字符串不能正确地在机器上读取,因此将其捕获到变量中的用途非常有限.我要花一小笔钱,您实际上只是想直接显示输出,而不是将其存储在变量中,这样您就可以echo其值:

This string isn't properly machine readable, and so it's of limited use to capture it in a variable at all. I'll wager a small sum of money that you actually simply want to display the output directly instead of storing it in a variable so that you can then echo its value:

cut -d, -f1 /filepath/*vendor.csv | sort -u

如果要遍历值,请将其进一步传递到while read -r ...

If you want to loop over the values, pipe this further to while read -r ...

这篇关于传递通配符以在Shell脚本中剪切命令并将其存储在变量中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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