使用 URL 作為命令和輸出檔案名稱的參數

使用 URL 作為命令和輸出檔案名稱的參數

我嘗試在一行中使用 XARGS 或 FOR 來接受列表網址作為參數,此操作會失敗,因為檔案名稱不能是 URL。我們嘗試使用 FOR 和 XARGS 執行此操作,但失敗了:

$for i in $(cat example.txt);do echo $i > $i; done
bash: http://example.site.com: No such file or directory
bash: https://secureexample.com: No such file or directory

$cat example.txt | xargs -I {} sh -c "echo  {} > {}"
sh: 1: cannot create http://example.site.com: Directory nonexistent
sh: 1: cannot create https://secureexample.com: Directory nonexistent

是否可以從第二次出現的參數(輸出文件的名稱)中刪除正斜杠,以便為每個主機獲得一個獨特的文件,例如:

http.example.site.com
https.secureexample.com

我對單行 bash 的了解非常有限,遺憾的是,這是公司的偏好。我嘗試在檔案名稱上使用大括號,並使用 SED、AWK 甚至 CUT 更改值,但正如我所說,我的知識太有限了。我還嘗試了 XARGS 和 FOR 的多個參數,但我也未能成功。

我能夠透過建立單獨的檔案來規避這個問題URI代替網址,然後補充http://和https://,但這意味著檔案加倍,命令加倍,例如:

$cat http_example.txt
example.site.com
secureexample.com 
$cat http_example.txt | xargs -I {} sh -c "echo  http://{} > {}"
$ls -la
-rw-r--r-- 1 root root   24 Mar 22 08:06 example.site.com
-rw-r--r-- 1 root root   25 Mar 22 08:06 secureexample.com

有沒有任何如何在使用帶有 URLS 的文件的同時,在單行 bash 命令中完成這項工作?謝謝你!

答案1

while read -r i;do echo "$i">"${i#http*://}";done<example.txt

這將刪除主要http(s)://部分、建立example.site.comsecureexample.com檔案。

這是一個從前導部分${i#http*://}刪除匹配的構造(是通配符)。$ihttp*://*


while read -r i;do echo "$i">"$(echo "$i"|sed 's|://|.|')";done<example.txt

這會創建文件http.example.site.comhttps.secureexample.com.

將輸入檔行sed替換為.://.

相關內容