美文网首页
Shell-新建的shell 脚本传入带有空格的参数

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

作者: VictorZhangWang | 来源:发表于2018-07-24 13:11 被阅读188次

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

相关文章

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

    新建的脚本,指定APP打开文件 执行过程中,发现带空格的参数总是不对,后来在脚本中取参数时都加上"",问题解决

  • shell脚本

    在为shell脚本传递的参数中如果包含空格,应该使用单引号或者双引号将该参数括起来,以便于脚本将这个参数作为整体来...

  • 03shell传递参数

    $n获取脚本参数 n为数字 在为shell脚本传递的参数中如果包含空格,应该使用单引号或者双引号将该参数括起来,以...

  • 【Linux shell学习笔记-02-特殊参数变量】

    在Linux shell中,在命令行执行shell脚本时,通过用空格间隔不同的值作为参数传递到脚本中执行相应的计算...

  • 2021-09-10

    python调用shell脚本 1. 传入参数 python文件 可以套用python传递参数,二次传递给shel...

  • shell 脚本传参 常用符号表示

    学习shell函数中... 1. `$1` 、..从1开始,表示参数对参数按传入顺序的引用 `$0` 表示脚本当前...

  • shell脚本或R脚本中参数含有空格的处理方法

    如图,shell脚本中参数含有空格或其他特殊字符,需要将参数使用单引号或双引号括起来 如果是R脚本,则可以将参数保...

  • shell基础(三)

    一、函数 二、函数的执行 小插曲 实战题目一: 用shell脚本检查某网站是否存在异常 实战题目二:参数传入脚本、...

  • shell中通过字符串执行函数

    需求描述: shell脚本中定义了很多功能函数,我要通过执行脚本传入参数执行函数,参数为函数名字, 解决方法 执行结果

  • Shell脚本中的参数

    脚本中给的各种参数 $#:传入脚本的参数个数; $0: 脚本自身的名称; $1: 传入脚本的第一个参数; $2...

网友评论

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

      本文链接:https://www.haomeiwen.com/subject/pyvemftx.html