Shell-新建的shell 腳本傳入帶有空格的參數(shù)

新建的腳本,指定APP打開文件

#!/bin/sh

printHelpDoc()
{
    printf "Usage: ./OpenFile.sh [-a <application>] [filenames] [--args arguments]\n"
    printf "Help: Open opens files from a shell.\n\tBy default, opens each file using the default application for that file. \n\tIf the file is in the form of a URL, the file will be opened as a URL.\n"
    printf "Options:\n"
    printf "\t-a      Opens with the specified application.\n\n\n"
}
#params count
paramCount=$#
printf "paramCount=$paramCount\n"

#是否包含第1個(gè)參數(shù)
if [ -z "$1" ]; then
    printf "Please enter the valid arguments\n"
    #printHelpDoc
    exit 1
elif  test $1 = "-a" 
 then
    if [ -z "$2" ]; then
        printf "Please enter the valid arguments\n"
        exit 1
    fi
fi


for (( i = 1; i <= paramCount; i++ )); do
    #statements
    params[i-1]="${!i}"
    echo ${params[i-1]}
done

firstArg="${params[0]}"
secondArg="${params[1]}"
index=0
if test $firstArg = "-a" ; then
    let index=2
fi

if [ $index -eq $paramCount ]; then
    printf "open $firstArg $secondArg\n"
    open "$firstArg" "$secondArg"
    else
          for (( i = index; i < paramCount; i++ )); do
              #statements
              arg="${params[i]}"
              if test $firstArg = "-a" ; then
                  printf "$i open $firstArg $secondArg $arg\n"
                  open "$firstArg" "$secondArg" "$arg"
              else
                  open "$arg"
              fi
          done
fi

執(zhí)行過程中,發(fā)現(xiàn)帶空格的參數(shù)總是不對(duì),后來在腳本中取參數(shù)時(shí)都加上"",問題解決


image.png
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 一、Python簡(jiǎn)介和環(huán)境搭建以及pip的安裝 4課時(shí)實(shí)驗(yàn)課主要內(nèi)容 【Python簡(jiǎn)介】: Python 是一個(gè)...
    _小老虎_閱讀 6,354評(píng)論 0 10
  • .bat腳本基本命令語(yǔ)法 目錄 批處理的常見命令(未列舉的命令還比較多,請(qǐng)查閱幫助信息) 1、REM 和 :: 2...
    慶慶慶慶慶閱讀 8,542評(píng)論 1 19
  • linux資料總章2.1 1.0寫的不好抱歉 但是2.0已經(jīng)改了很多 但是錯(cuò)誤還是無(wú)法避免 以后資料會(huì)慢慢更新 大...
    數(shù)據(jù)革命閱讀 13,344評(píng)論 2 33
  • 章 我—叫“紙鳶” 這是父親對(duì)我的期望,他想讓我不受約束,去做自己想做的事, 就像斷線風(fēng)箏一樣,不...
    二月cfgb閱讀 235評(píng)論 0 1
  • 今天說六氣,六氣就是風(fēng)、寒丶君火丶濕丶燥、相火的簡(jiǎn)稱 六氣一般又以三陰三陽(yáng)為主結(jié)合十二地支來說明每年天地人的變化 ...
    三通閱讀 510評(píng)論 0 1

友情鏈接更多精彩內(nèi)容