Shell-新建的shell 脚本传入带有空格的参数

新建的脚本,指定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个参数
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

执行过程中,发现带空格的参数总是不对,后来在脚本中取参数时都加上"",问题解决


image.png
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 一、Python简介和环境搭建以及pip的安装 4课时实验课主要内容 【Python简介】: Python 是一个...
    _小老虎_阅读 5,934评论 0 10
  • .bat脚本基本命令语法 目录 批处理的常见命令(未列举的命令还比较多,请查阅帮助信息) 1、REM 和 :: 2...
    庆庆庆庆庆阅读 8,299评论 1 19
  • linux资料总章2.1 1.0写的不好抱歉 但是2.0已经改了很多 但是错误还是无法避免 以后资料会慢慢更新 大...
    数据革命阅读 12,270评论 2 33
  • 章 我—叫“纸鸢” 这是父亲对我的期望,他想让我不受约束,去做自己想做的事, 就像断线风筝一样,不...
    二月cfgb阅读 223评论 0 1
  • 今天说六气,六气就是风、寒丶君火丶湿丶燥、相火的简称 六气一般又以三阴三阳为主结合十二地支来说明每年天地人的变化 ...
    三通阅读 462评论 0 1