Can't pass a variable representing the output of lsb_release to a docker dontainer

I don't know why, but the rendering of my code mucks up the spacing and indentation, despite being correct in the original file. I'm having issues getting the following script to run (specifically the nested script at the end of the docker command near the end of the script; I think I'm not passing the `lsb_release` variable correctly as an argument to the called `create_rpc_rs.sh` script, but I don't know how to correctly do so):

#!/bin/sh

# This script regenerates the `src/rpc_proto.rs` file from `rpc.proto`.

lsb_release=$(lsb_release -si);
echo"$lsb_release"
echo"Checking if docker is installed."
if docker --version | grep -q "Docker version";then
echo"Confirmed that docker is installed."
else
echo"Docker is not installed; attempting to install now."
if [[ "$lsb_release"=*Linux* ]];then
case"$lsb_release"in*Ubuntu*)
            apt-get update;
            apt-get install -y docker;;
*Manjaro*) sudo pacman -S --needed docker;;
*)
echo"installation for Docker is not configured for \
            your OS, please install manually and make a pull \
            request to install in this script for your OS">&2;
exit 1;;
esac;
fi
fi

echo"Checking that docker is running."
if systemctl status docker | grep -q 'Active: inactive (dead)';then
echo"docker is not running; starting docker now."
    systemctl start docker
else
echo"Confirmed that docker is running."
fi

docker run --rm -v "$(pwd)":/usr/code:z -w /usr/code rust \
    /bin/bash -c "sh create_rpc_rs.sh $lsb_release"

mv -f rpc.rs ./src/rpc_proto.rs


This is create_rpc_rs.sh:

#!/bin/sh

echo"Installing protobuf if not already installed."
if [[ $1=*Linux* ]] ;then
case$1in*Ubuntu*)
        apt-get update;
        apt-get install -y protobuf-compiler;;
*Manjaro*) sudo pacman -S --needed protobuf;;
*)
echo"installation for protobuf is not configured for \
            your OS, please install manually and make a pull request;"
# exit 1;;
esac;
fi
echo"installing protobuf";
cargo install --version 1 protobuf;
echo"Producing rpc.rs via protoc and rpc.proto"
 protoc --rust_out . rpc.proto



This is the output when running `./regen_proto_structs.sh`, as you can see, `lsb_release` is not found, and I don't know how to correctly pass it Ii.e. the output of lsb_release -si on the computer that docker is runnning on, to the docker container.:

$ ./regen_structs_proto1.sh
Checking if docker is installed.
Confirmed that docker is installed.
Checking that docker is running.
Confirmed that docker is running.
Installing protobuf if not already installed.
create_rpc_rs.sh: 4: create_rpc_rs.sh: [[: not found
installing protobuf
    Updating registry `https://github.com/rust-lang/crates.io-index`

Unfortunately I posted this at a bad time as I need to stop working for the day.

Welcome to the forum.

Please become accustomed to provide decent context info of your problem.

It is always helpful to carefully and detailedly phrase a request, and to support it with system info like OS and shell, related environment (variables, options), preferred tools, adequate (representative) sample input and desired output data and the logics connecting the two including your own attempts at a solution, and, if existent, system (error) messages verbatim, to avoid ambiguities and keep people from guessing.

Thanks for applying the required code tags, but don't (over)use formatting and colouring, leaving the result nearly unreadable.

Just guessing: the first code shown is regen_structs_proto1.sh , and the second is create_rpc_rs.sh ? Would have been nice to have pointed out in the first place.

Your juggling with shells and "shebangs" is sort of inconsistent, use bash throughout, or use sh , consistently. That might also be the source of the error: sh doesn't provide the [[ "conditional command".

For your ultimate question: export a variable to make it accessible in subshells / scripts.

1 Like

Thanks. I have ditched this approach to modifying these scripts as per comments from the maintainers of the project. "I'm not sure we want to complexify the scripts, as these scripts are not meant to be run as long as you don't modify the .proto files."