BASH变量,受数字文件和文件夹名称影响的命令 [英] BASH variables, commands affected by numeric-numbered file and folder names

查看:68
本文介绍了BASH变量,受数字文件和文件夹名称影响的命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题

我一直在运行Linux命令,这些命令在包含数字编号文件和文件夹的文件夹中运行;例如,文件顺序编号为1、2、3 ...

例如,如果我所在的文件夹中包含命令中出现的带有数字名称的文件或文件夹,则该命令输出的输出可能会被截断.

以下是一些示例:

  $ ls -l共8个drwxr-xr-x 2维多利亚维多利亚4096年5月7日18:34 1drwxr-xr-x 2维多利亚维多利亚4096年5月7日18:14 2-rw-r--r-- 1维多利亚维多利亚0 May 7 18:34 3## 失败$ a ="[CPT1A] A Selective";回声$ a1 A选择性$ b ="[CPT2A]选择性";回声$ b2 A选择性$ c ="[CPT3A] A Selective";回声$ c2 A选择性...## 经过$ d ="[CPT4A] A Selective";回声$ d[CPT4A]选择性 


更新/解决方案

...每个可接受的答案:使用时引用BASH变量.

  $ a ="[CPT1A]选择性";回声$ a1 A选择性$ a ="[CPT1A] A Selective";回声"$ a"[CPT1A]选择性 

解决方案

问题是您在使用变量时没有引用该变量-也就是说,您正在使用 echo $ a 而不是 echo"$ a" .当引用变量时不带引号时,它将分成单词(因此"[CPT1A] A Selective"变成"[CPT1A]""A""Selective"),然后每个单词中都包含类似于文件名的内容通配符将扩展为匹配文件名的列表.

方括号表达式(例如 [CPT1A] )实际上是有效的通配符表达式,它们匹配其中的任何单个字符,因此,如果存在名为"A","C","P","T"或"1",它将扩展为匹配的名称.如果没有,通配符表达式将原封不动地传递.

解决方案:双引号变量引用可避免出现此类意外情况.用 $()(或反引号,但不要使用那些反引号)进行命令替换也是如此.在某些地方可以安全地将其保留下来,例如直接任务,但是IMO与在各处跟踪使用它们相比,保持跟踪更为安全.例如, a = $ b 可以,但是 a ="$ b" 也可以.另一方面, export a = $ b 可能会或可能不会(取决于您使用的是哪个shell),但是 export a ="$ b" 起作用.

顺便说一句, shellcheck.net 擅长指出这些问题(以及其他一些常见错误)./p>

Issue

I have been experiencing issues with Linux commands run in folders that contain numerically numbered files and folders; e.g., files sequentially numbered 1, 2, 3 ...

For example, if I am in a folder that contains a file or folder with a numeric name that appears in my command, the output from that command output might be truncated.

Here are some examples:

$ ls -l
total 8
drwxr-xr-x 2 victoria victoria 4096 May  7 18:34 1
drwxr-xr-x 2 victoria victoria 4096 May  7 18:14 2
-rw-r--r-- 1 victoria victoria    0 May  7 18:34 3

## fail
$ a="[CPT1A] A Selective"; echo $a
1 A Selective
$ b="[CPT2A] A Selective"; echo $b
2 A Selective
$ c="[CPT3A] A Selective"; echo $c
2 A Selective
...

## pass
$ d="[CPT4A] A Selective"; echo $d
[CPT4A] A Selective


Update/solution

... per accepted answer: quote the BASH variable, when used.

$ a="[CPT1A] A Selective"; echo $a
1 A Selective
$ a="[CPT1A] A Selective"; echo "$a"
[CPT1A] A Selective

解决方案

The problem is that you aren't quoting the variable when you use it -- that is, you're using echo $a instead of echo "$a". When a variable is referenced without quotes, it gets split into words (so "[CPT1A] A Selective" becomes "[CPT1A]" "A" "Selective"), and then each of those words that contains anything that looks like a filename wildcard gets expanded into a list of matching filenames.

Square-bracket expressions like [CPT1A] are actually valid wildcard expressions that match any single character within them, so if there are files named "A", "C", "P", "T", or "1", it would expand to the matching names. If there aren't any, the wildcard expression just gets passed through intact.

Solution: double-quote variable references to avoid surprises like this. The same goes for command substitutions with $( ) (or backticks, but don't use those). There are a few places where it's safe to leave them off, like in a direct assignment, but IMO it's safer to just use them everywhere than to try to keep track of the exceptions. For example, a=$b is ok, but so is a="$b". On the other hand, export a=$b might or might not work (depending on which shell you're using), but export a="$b" will work.

BTW, shellcheck.net is good at pointing these out (along with some other common mistakes).

这篇关于BASH变量,受数字文件和文件夹名称影响的命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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