Ole
Ole

Reputation: 47088

Accessing the number of arguments pass into a docker run command script

I have a docker Entrypoint script that looks like this:

#!/bin/sh
LABEL=$1
mkdir -p /backup/$LABEL
...

I can access the arguments passed in the normal bash way via $1, $2, etc. but I also need to know the number of arguments passed in. At first I thought I could do this like this:

if [ $# -eq 2 ];
  then

However that does not work. Any ideas on how to retrieve the number of arguments?

TIA, Ole

Upvotes: 0

Views: 389

Answers (2)

Ole
Ole

Reputation: 47088

OK - In reality nothing passed in was resolving. The reason is that the entrypoint line needs to look like this:

ENTRYPOINT ["bash", "/run.sh"]

Mine looked like this:

ENTRYPOINT ["/run.sh"]

See here for more info: Referencing the first argument passed to the docker entrypoint?

Upvotes: 0

pneumatics
pneumatics

Reputation: 2885

Weird. This should work. But, if you can read the positional parameters $1 and $2, you may have luck looping over them:

#!/bin/bash

params="$@"
while param=$1 && [ -n "$param" ]
do
    shift
    ((count += 1))
    echo "here comes $param"
done

echo "All params: ${params[@]}"
echo "We saw $count of them"

Upvotes: 1

Related Questions