Linux 板


LINE

分享一下 bash -c 的參數傳遞方式! ----- 前言 ----- 原本想利用 Dolphin Service Menu ,可以在檔案管理器中對選擇檔案執行處理 (例如 jpg2avif )。 結果 .desktop 裡面無法直接指定腳本,顯示「execvp: 可執行檔格式錯誤」 (在研究許久後才發現原來 .sh 檔頭我漏了「!」,打成「#/bin/bash」 XD .desktop 這樣設定是正確的,卡很久是因為不知道 Dolphin 到底會怎樣處理腳本) Exec=img2avif.sh %F 不過若是改由以下方式就能正常執行 Exec=bash -c 'img2avif.sh "%f"' 上面會有幾個問題: 1. %f 只會傳單一檔案 2. %F 可以傳多個檔案的路徑,但一旦遇到路徑包含空格就會被拆開。 3. 如果 %F 放在引號外面, sh 無法接收到任何參數。 再仔細研究一下 bash -c 的語法,才發現關於參數傳遞我還不夠了解! ----- bash -c 參數傳遞 ----- man bash: ----------- -c If the -c option is present, then commands are read from the first non-option argument command_string. If there are arguments after the command_string, the first argument is assigned to $0 and any remaining arguments are assigned to the positional parameters. The assignment to $0 sets the name of the shell, which is used in warning and error messages. ----------- 簡單說, "-c command_string" 後面的參數會傳入 command_string,並且是從 $0 開始。 因此如果要傳給 command_string 內的 .sh 必須再加上 "$@" 結論就是; .desktop 中設定(如果要透過 bash -c)應該如下: Exec=bash -c 'img2avif.sh "$@" ' 'dummy-param' %F 下面是一些測試,不太懂的看看應該能明白。 ----------------------- ## 準備同一份指令給 -c 與 .sh $ cmd='echo argc=$#, \$0=\"$0\", \$1=\"$1\", \$2=\"$2\"' $ echo -e '#!/bin/bash \n'"$cmd" > a.sh $ chmod u+x a.sh ## 用 bash 直接執行 .sh $ bash a.sh dd 11 "2 2" argc=3, $0="a.sh", $1="dd", $2="11" ## 透過 -c 執行 command_string $ bash -c "$cmd" dd 11 "2 2" argc=2, $0="dd", $1="11", $2="2 2" ## 透過 -c 執行 .sh (此為正確方式) $ bash -c '/fullPathTo/a.sh "$@" ' dd 11 "2 2" argc=2, $0="/fullPathTo/a.sh", $1="11", $2="2 2" $ bash -c '/fullPathTo/a.sh "$*" ' dd 11 "2 2" argc=1, $0="/fullPathTo/a.sh", $1="11 2 2", $2="" $ bash -c '/fullPathTo/a.sh $* ' dd 11 "2 2" argc=3, $0="/fullPathTo/a.sh", $1="11", $2="2" ----------------------- note: * "$@" is equivalent to "$1" "$2" ... * Dolphin Service Menus https://develop.kde.org/docs/apps/dolphin/service-menus/ * Desktop Entry Specification https://specifications.freedesktop.org/desktop-entry-spec/latest * 單純轉檔的話可以直接用 magick 就好(搭配 gnu parallel 更完美)。 這裡是假設需要做複雜處理。 * 也可以將指令壓縮為單行放進 command_string 執行,這比較適合不想分兩個檔 或想便於發佈的情況。 --
QR Code



※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 101.12.88.235 (臺灣)
※ 文章網址: https://webptt.com/m.aspx?n=bbs/Linux/M.1699182818.A.AB4.html ※ 編輯: sppmg (101.12.88.235 臺灣), 11/05/2023 19:25:17
1F:推 microloft: 都寫進 .sh 了,其實可以不用 bash -c 多包一層 11/06 01:09
2F:→ sppmg: 因為我一開始沒辦法直接執行sh啊!XD 11/06 06:28
3F:→ sppmg: 如果哪天遇到上層不會自動用sh執行腳本的話可以這樣指定。 11/06 06:30
4F:推 brli7848: 到那時候,直接指定#!/bin/bash就好了吧? 11/06 10:20







like.gif 您可能會有興趣的文章
icon.png[問題/行為] 貓晚上進房間會不會有憋尿問題
icon.pngRe: [閒聊] 選了錯誤的女孩成為魔法少女 XDDDDDDDDDD
icon.png[正妹] 瑞典 一張
icon.png[心得] EMS高領長版毛衣.墨小樓MC1002
icon.png[分享] 丹龍隔熱紙GE55+33+22
icon.png[問題] 清洗洗衣機
icon.png[尋物] 窗台下的空間
icon.png[閒聊] 双極の女神1 木魔爵
icon.png[售車] 新竹 1997 march 1297cc 白色 四門
icon.png[討論] 能從照片感受到攝影者心情嗎
icon.png[狂賀] 賀賀賀賀 賀!島村卯月!總選舉NO.1
icon.png[難過] 羨慕白皮膚的女生
icon.png閱讀文章
icon.png[黑特]
icon.png[問題] SBK S1安裝於安全帽位置
icon.png[分享] 舊woo100絕版開箱!!
icon.pngRe: [無言] 關於小包衛生紙
icon.png[開箱] E5-2683V3 RX480Strix 快睿C1 簡單測試
icon.png[心得] 蒼の海賊龍 地獄 執行者16PT
icon.png[售車] 1999年Virage iO 1.8EXi
icon.png[心得] 挑戰33 LV10 獅子座pt solo
icon.png[閒聊] 手把手教你不被桶之新手主購教學
icon.png[分享] Civic Type R 量產版官方照無預警流出
icon.png[售車] Golf 4 2.0 銀色 自排
icon.png[出售] Graco提籃汽座(有底座)2000元誠可議
icon.png[問題] 請問補牙材質掉了還能再補嗎?(台中半年內
icon.png[問題] 44th 單曲 生寫竟然都給重複的啊啊!
icon.png[心得] 華南紅卡/icash 核卡
icon.png[問題] 拔牙矯正這樣正常嗎
icon.png[贈送] 老莫高業 初業 102年版
icon.png[情報] 三大行動支付 本季掀戰火
icon.png[寶寶] 博客來Amos水蠟筆5/1特價五折
icon.pngRe: [心得] 新鮮人一些面試分享
icon.png[心得] 蒼の海賊龍 地獄 麒麟25PT
icon.pngRe: [閒聊] (君の名は。雷慎入) 君名二創漫畫翻譯
icon.pngRe: [閒聊] OGN中場影片:失蹤人口局 (英文字幕)
icon.png[問題] 台灣大哥大4G訊號差
icon.png[出售] [全國]全新千尋侘草LED燈, 水草

請輸入看板名稱,例如:BuyTogether站內搜尋

TOP