I don't want to source my.sh script every time before I start packer build command... because I always forget to do this. These tasks are repeatable and it makes sense to create a shell script for that.
Problem : If the command
$source env.sh
was executed once, I don't want to execute this command again but continue with the others. Is there any solution for that? My script:
#!/bin/bash
set -e
echo "Today is $(date)"
echo "--------------------"
sleep 1.5
echo "1. Pass env variables"
source env.sh
echo "2. Check the configuration of Packer template"
packer validate example.pkr.hcl
echo "3. Build the image"
#packer build example.pkr.hcl
Set an variable inside your script, then test for the presence of that variable at the top of the script. For example:
#!/bin/bash
if [ "${ALREADY_LOADED}" -ne "YES" ]; then
...
# Put commands here
...
ALREADY_LOADED=YES
fi
If you want this to persist to child processes, then use an environment variable instead of a local one, by export
ing it. But be aware: Some things are not inherited by child processes. For example, if your script sets an array variable, the child will not be able to see the array. So you may want to leave some commands outside the if...then...fi
clause.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.