簡體   English   中英

部分文件名作為Shell腳本輸入

[英]Partial File Name as Shell Script Input

我正在嘗試創建一個將部分文件名作為輸入的shell腳本,然后對具有匹配名稱的文件進行操作。 例如,我有三個文件sample1.txt,sample2.txt and sample3.txt ,這是我的腳本

#!bin/bash

VALUE=$1
VALUE2=$2
FILE_NAME=$3

echo $FILE_NAME

我用這個命令運行它

sh myscript.sh arg1 arg2 sample*

但是我得到這個作為輸出

sample3.txt (sample2.txt)

但是我想要的是

sample1.txt
sample2.txt
sample3.txt

我該怎么辦?

sample*

get擴展為(如果文件存在,則不再具有該名稱的文件,等等):

sample1.txt sample2.txt sample3.txt

所以當你寫:

sh myscript.sh arg1 arg2 sample*

您實際編寫的腳本看到的是:

sh myscript.sh arg1 arg2 sample1.txt sample2.txt sample3.txt

您的腳本得到5個參數,而不是3個

那么你也能:

#!bin/bash

VALUE=$1
VALUE2=$2

# shift the arguments to jump over the first two 
shift 2

# print all the rest of arguments
echo "$@"

# ex. read the arguments into an array
FILE_NAME=("$@")
echo "${FILE_NAME[@]}"

jdoodle的實時示例。

暫無
暫無

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

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