Dateinamenerweiterung für Expect-Skript innerhalb von Bash

Dateinamenerweiterung für Expect-Skript innerhalb von Bash

Ich habe das folgende Bash-Skript mit einem eingebetteten Expect-Skript:

#!/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
}

Im Ordner befinden sich mehrere Dateien im Format{x}0AS_26x86_64_{x.x.x}_{rev}.bin

Wenn ich das Skript ausführe, erhalte ich beim ersten Echo den richtigen Dateinamen. Aber wenn ich versuche, diesen mit an das Expect-Skript zu übergeben ${filename}, ist die Dateinamenerweiterung verschwunden.

Beispielausgabe:

# ./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"

Wie Sie sehen, $filenamewird es beim Echo korrekt angezeigt, jedoch nicht im Expect-Teil.

Bearbeiten:

Führen Sie das Skript einfach mit -x aus, und es sieht so aus, als ob die Dateinamenvariable nie die vollständige Dateinamenerweiterung erhält, sondern nur das 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

Antwort1

Sie weisen der Variable nie wirklich einen bestimmten Dateinamen zu. Sie setzen die Variable auf ein Glob-Muster. Wenn Sie die Variable dann an übergeben echo, wird das Glob-Muster erweitert, sodass Sie den Namen Ihrer Datei gedruckt sehen. Die Variable wurde jedoch nie auf einen bestimmten Dateinamen gesetzt.

Sie benötigen also eine bessere Möglichkeit, den Dateinamen zu ermitteln. So etwas wie:

#!/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
}

verwandte Informationen