Linux 在ls输出中包括换行符?
练习一些Bash脚本:Linux 在ls输出中包括换行符?,linux,bash,shell,scripting,Linux,Bash,Shell,Scripting,练习一些Bash脚本: 1 #!/bin/bash 2 3 read -p "Input the path to a file or directory: " TARGET_PATH 4 5 if [ -f "${TARGET_PATH}" ] 6 then 7 echo "${TARGET_PATH} is a file!" 8 echo $(ls ${TARGET_PATH} -l) 9 elif [ -d "${TARGET_PATH
1 #!/bin/bash
2
3 read -p "Input the path to a file or directory: " TARGET_PATH
4
5 if [ -f "${TARGET_PATH}" ]
6 then
7 echo "${TARGET_PATH} is a file!"
8 echo $(ls ${TARGET_PATH} -l)
9 elif [ -d "${TARGET_PATH}" ]
10 then
11 echo "${TARGET_PATH} is a directory!"
12 echo $(ls ${TARGET_PATH} -l)
13 else
14 echo "${TARGET_PATH} is not a file or directory."
15 fi
如果TARGET\u PATH
是一个文件,那么ls-l
命令可以很好地对其运行
但是如果它是一个目录,ls-l
的输出将许多项压缩到一行中
在该实例中,我尝试迭代使用ls-l
命令,但它将所有内容按空格分割,因此每一部分都会出现在一个新行上
有没有一种方法可以输出与我们在终端输入
ls-l
时所期望的内容类似的内容?使用-1
选项(这是一个数字,而不是小写l)到ls
将使它每行输出一项
ls -1l "${TARGET_PATH}"
将
echo$(ls${TARGET\u PATH}-l)
更改为ls-l“${TARGET\u PATH}”
@anubhava当然!!谢谢,我完全忘记了我可以直接写命令,就像我在终端上写的一样