簡體   English   中英

將來自Linux“ ls”命令的文件名存儲到單獨的數組/列表元素中

[英]Store filenames from linux “ls” command into separate array/list elements

如何將文件名及其關聯的擴展名“僅”存儲到“ bash”腳本中的數組或列表中? 以某種方式將每個文件名存儲在單獨的元素中,而不會產生其他文件信息,例如創建日期或權限級別……

我喜歡做:

filelist=`ls -1 /somedir/`

然后遍歷$ filelist。

ls -1僅顯示文件名,不包含任何其他屬性。

像這樣:

root@myserver-1-00:~# filelist=($(ls))
root@myserver-1-00:~# echo $filelist
Desktop
root@myserver-1-00:~# echo ${filelist[0]}
Desktop
root@myserver-1-00:~# echo ${filelist[1]}
Documents
root@myserver-1-00:~# echo ${filelist[2]}
Downloads

variable = {$(yourcommand))->使輸出被分配為數組

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM