I’m new to bash testing and wanted to create a docker image to test a cli. I discovered ‘expect’ and wanted to try it against this example bash script. Only problem is its failing to spawn a file.
data/que.sh
#!/bin/bash
echo "Enter your name"
read $REPLY
echo "Enter your age"
read $REPLY
echo "Enter your salary"
read $REPLY
data/test.sh
#!/usr/bin/expect -f
set timeout 2
spawn /data/que.sh
expect "Enter your namer"
send -- "I am Stever"
expect "Enter your ager"
send -- "2r"
expect "Enter your salaryr"
send -- "10kr"
expect eof
docker-compose.yml
services:
linux-expect:
container_name: cont-alpine-linux-expect
image: img-alpine-linux-expect
build:
context: .
Dockefile
FROM alpine:latest
RUN apk add --update nodejs-current npm
USER root
RUN apk update && apk add expect
RUN mkdir data
COPY data /data
CMD ["node", "-e","setTimeout(() => console.log(999999), 99999999)"]
# Just run temporary timeout so we can interact with script
terminal command
- docker-compose up –build
new terminal
- docker exec -it cont-alpine-linux-expect sh
- expect data/test.sh
I am getting an error to find this file, but it does exist.
I am the root user.
Error
"spawn /data/que.sh"
(file "data/test.sh" line 4)
2
Answers
The problem is that it can’t find
bash
. You haveas the first line in your script, so the shell looks for bash to run it. But bash isn’t installed in Alpine images.
You can see if your script runs with
/bin/sh
. I believe that’s theash
shell in Alpine images. If that doesn’t work, you can installbash
in the image using apk.This is wrong:
the
read
command takes variable names as arguments.After the first echo, because REPLY is empty, you have the bare
read
, which will use the default REPLY variable.After the second echo, bash sees
read I am Steve
I
variable gets the value 2am
andSteve
variables get empty values.After the third echo, because REPLY did not get used after the previous
read
, you have againread I am Steve
I
variable gets the value "10k"am
andSteve
variables get empty values.