简体   繁体   English

在shell脚本中将变量子目录分配给输出命令

[英]Assign variable subdirectory to output command in the shell script

I'm trying to implement shell script to get the list of files of subdirectory: 我正在尝试实现shell脚本以获取子目录的文件列表:

#!/bin/bash
PATH="/Movies"
MOVIES=${PWD}${PATH}
LS="$(ls -lp) ${MOVIES}"

But I'm getting this error: ls: command not found 但我收到此错误:ls:找不到命令

How can add the subdirectory to the ls command? 如何将子目录添加到ls命令中? I'll really appreciate your help. 非常感谢您的帮助。

in setting a PATH variable in your script, you have overwritten the default PATH variable that tells your shell where your common binaries are located. 在脚本中设置PATH变量时,您已经覆盖了默认PATH变量,该变量告诉外壳程序公共二进制文件的位置。 Either change your PATH variable name, or use the full ls path '/bin/ls' 更改您的PATH变量名称,或使用完整的ls路径'/ bin / ls'

#!/bin/bash
PATH="/Movies"
MOVIES=${PWD}${PATH}
LS="/bin/ls -lp ${MOVIES}"

$LS

or 要么

#!/bin/bash
NOTPATH="/Movies"
MOVIES=${PWD}${NOTPATH}
LS="ls -lp ${MOVIES}"

$LS

more info here 更多信息在这里

It's also a good idea to not use UPPERCASE variables in shell scripts, as they're the default pattern for system wide environment variables. 最好不要在Shell脚本中使用UPPERCASE变量,因为它们是系统范围环境变量的默认模式。 This way you will avoid overwriting some variable with your own. 这样,您将避免用自己的变量覆盖某些变量。 If you must, run the env command to see what variables are set in your bash session. 如果需要,请运行env命令以查看在bash会话中设置了哪些变量。

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

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