![Expansión de nombre de archivo para script esperado dentro de bash](https://rvso.com/image/76421/Expansi%C3%B3n%20de%20nombre%20de%20archivo%20para%20script%20esperado%20dentro%20de%20bash.png)
Tengo el siguiente script bash con un script expect incorporado:
#!/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
}
En la carpeta hay varios archivos en el formato{x}0AS_26x86_64_{x.x.x}_{rev}.bin
Cuando ejecuto el script, obtengo el nombre de archivo correcto en el primer eco. Pero cuando intento pasar esto al script esperado usando ${filename}
, la expansión del nombre de archivo desaparece.
Salida de muestra:
# ./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"
Como puede ver, los $filename
programas son correctos con el eco, pero no dentro de la parte esperada.
Editar:
Simplemente ejecute el script con -x y parecerá que la variable de nombre de archivo nunca obtiene la expansión completa del nombre de archivo, solo el eco lo hace.
# ./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
Respuesta1
En realidad, nunca asignas un nombre de archivo específico a la variable. Lo que estás haciendo es configurar la variable en un patrón global. Luego, cuando pasa la variable a echo
, el patrón global se expande, por lo que ve el nombre de su archivo impreso. Sin embargo, la variable nunca se ha establecido con un nombre de archivo específico.
Por lo tanto, necesita una mejor manera de obtener el nombre del archivo. Algo como:
#!/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
}