bash中的Expect脚本的文件名扩展

德克里斯

我有以下带有嵌入式Expect脚本的bash脚本:

#!/bin/bash

if [ ! $# == 2 ]
  then
    echo "Usage: $0 os_base_version sn_version"
    exit
fi

if [ -e /some/file/path/$10AS_26x86_64_$2_*.bin ]
  then
    filename="/some/file/path/$10AS_26x86_64_$2_*.bin"
    echo ${filename}
else
  echo "install archive does not exist."
  exit
fi

{
  /usr/bin/expect << EOD
  set timeout 20
  spawn "${filename}"

  expect {
      "Press Enter to view the End User License Agreement" {
          send "\r"
          exp_continue
      }
      "More" {
          send " "
          exp_continue
      }
      "Do you accept the End User License Agreement?" {
          send "y\r"
      }
  }
  interact
  expect eof
EOD
}

文件夹中有几个文件,格式为 {x}0AS_26x86_64_{x.x.x}_{rev}.bin

运行脚本时,在第一个回显中获取正确的文件名。但是,当我尝试使用将它传递给期望脚本时${filename},文件名扩展消失了。

样本输出:

# ./make.sh 2 1.2.3
/some/file/path/20AS_26x86_64_1.2.3_45678.bin
spawn /some/file/path/20AS_26x86_64_1.2.3_*.bin
couldn't execute "/some/file/path/20AS_26x86_64_1.2.3_*.bin": no such file or directory
    while executing
"spawn /some/file/path/20AS_26x86_64_1.2.3_*.bin"

正如您所看到的,$filename显示与回声是正确的,但在期望部分中却没有。

编辑:

只需使用-x运行脚本,看起来文件名变量就永远不会得到完整的文件名扩展,只有echo可以。

# ./make.sh 2 1.2.3
+ '[' '!' 2 == 2 ']'
+ '[' -e /some/file/path/20AS_26x86_64_1.2.3_45678.bin ']'
+ filename='/some/file/path/20AS_26x86_64_1.2.3_*.bin'
+ echo /some/file/path/20AS_26x86_64_1.2.3_45678.bin
/some/file/path/20AS_26x86_64_1.2.3_45678.bin
+ exit
芒登

您实际上从未为该变量分配特定的文件名。您正在做的是将变量设置为全局模式。然后,将变量传递给时echo,将扩展全局模式,因此您看到的文件名已被整理。但是,该变量从未设置为特定的文件名。

因此,您需要一种更好的获取文件名的方法。就像是:

#!/bin/bash

## Make globs that don't match anything expand to a null
## string instead of the glob itself
shopt -s nullglob
## Save the list of files in an array
files=( /some/file/path/$10AS_26x86_64_$2_*.bin )
## If the array is empty
if [ -z $files ]
then
    echo "install archive does not exist."
    exit
## If at least one file was found    
else
    ## Take the first file 
    filename="${files[0]}"
    echo "$filename"
fi

{
  /usr/bin/expect << EOD
  set timeout 20
  spawn "${filename}"

  expect {
      "Press Enter to view the End User License Agreement" {
          send "\r"
          exp_continue
      }
      "More" {
          send " "
          exp_continue
      }
      "Do you accept the End User License Agreement?" {
          send "y\r"
      }
  }
  interact
  expect eof
EOD
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

在Bash脚本中解析文件名

来自分类Dev

在bash shell脚本中,如何重命名多个文件,仅保留数字和原始文件名的扩展名?

来自分类Dev

Bash Shell脚本从文件名中查找丢失的文件

来自分类Dev

如何在bash中引用glob扩展文件名?

来自分类Dev

在Bash脚本中获取输出文件名

来自分类Dev

如何在bash脚本中验证文件名?

来自分类Dev

bash脚本中的命令选项混淆了文件名

来自分类Dev

Bash Shell脚本删除文件名中的GUID

来自分类Dev

bash脚本无法处理文件名中的空格

来自分类Dev

bash 脚本中的 curl 添加 ? 在文件名的末尾

来自分类Dev

传递给 bash 脚本的文件名中的多个空格

来自分类Dev

文件名参数扩展bash

来自分类Dev

限制Bash文件名扩展

来自分类Dev

Bash中的“文件名扩展”和“路径名扩展”是否相同?

来自分类Dev

Expect脚本-发送带有变量的文件名

来自分类Dev

文件名中的空格“破坏”文件名脚本

来自分类Dev

在Bash中搜索包含特定字符串的特定扩展名文件名

来自分类Dev

如何在bash终端中从%f获取不带扩展名的文件名?

来自分类Dev

如何在bash中检索文件名或扩展名

来自分类Dev

如何在bash终端中从%f获取不带扩展名的文件名?

来自分类Dev

Bash脚本从文件夹中的所有文件名中删除字符

来自分类Dev

Bash脚本从文件夹中的所有文件名中删除字符

来自分类Dev

复制bash脚本文件,并在文件名中添加其他字符

来自分类Dev

为什么在Arch Linux中bash会添加空格并扩展文件名?

来自分类Dev

Bash大括号扩展以删除部分文件名

来自分类Dev

Bash如何知道何时进行文件名扩展?

来自分类Dev

Bash 5文件名扩展和nullglob

来自分类Dev

从bash中的完整文件路径获取父目录名称和文件名而没有扩展名?

来自分类Dev

使用bash我需要在文件名中的文件扩展名之前删除尾随空格

Related 相关文章

  1. 1

    在Bash脚本中解析文件名

  2. 2

    在bash shell脚本中,如何重命名多个文件,仅保留数字和原始文件名的扩展名?

  3. 3

    Bash Shell脚本从文件名中查找丢失的文件

  4. 4

    如何在bash中引用glob扩展文件名?

  5. 5

    在Bash脚本中获取输出文件名

  6. 6

    如何在bash脚本中验证文件名?

  7. 7

    bash脚本中的命令选项混淆了文件名

  8. 8

    Bash Shell脚本删除文件名中的GUID

  9. 9

    bash脚本无法处理文件名中的空格

  10. 10

    bash 脚本中的 curl 添加 ? 在文件名的末尾

  11. 11

    传递给 bash 脚本的文件名中的多个空格

  12. 12

    文件名参数扩展bash

  13. 13

    限制Bash文件名扩展

  14. 14

    Bash中的“文件名扩展”和“路径名扩展”是否相同?

  15. 15

    Expect脚本-发送带有变量的文件名

  16. 16

    文件名中的空格“破坏”文件名脚本

  17. 17

    在Bash中搜索包含特定字符串的特定扩展名文件名

  18. 18

    如何在bash终端中从%f获取不带扩展名的文件名?

  19. 19

    如何在bash中检索文件名或扩展名

  20. 20

    如何在bash终端中从%f获取不带扩展名的文件名?

  21. 21

    Bash脚本从文件夹中的所有文件名中删除字符

  22. 22

    Bash脚本从文件夹中的所有文件名中删除字符

  23. 23

    复制bash脚本文件,并在文件名中添加其他字符

  24. 24

    为什么在Arch Linux中bash会添加空格并扩展文件名?

  25. 25

    Bash大括号扩展以删除部分文件名

  26. 26

    Bash如何知道何时进行文件名扩展?

  27. 27

    Bash 5文件名扩展和nullglob

  28. 28

    从bash中的完整文件路径获取父目录名称和文件名而没有扩展名?

  29. 29

    使用bash我需要在文件名中的文件扩展名之前删除尾随空格

热门标签

归档