What's wrong with my bash script? I'm trying to pass positional parameters within a function. My last test - Test 4 works but its basically the command that I would run on the command line, no variable substitution.
I would like to call my function. Can someone tell me if the construction of my first 3 tests are valid and how to I can correct them? Thanks!
To execute: ./myscript.sh dev01 tester
#!/bin/bash
set +x
if [[ $# != 2 ]]; then
echo "Usage: ./script.sh <ENV> <COMPONENT>"
exit 1
fi
# Setup VARS
CREDS="-x foobar -a ec2.local_ipv4"
ENVIRONMENT="$1"
ROLES="$2"
function deploy {
knife ssh "$CREDS" "chef_environment:"$ENVIRONMENT" AND roles:*"$ROLES"*" "uname"
}
echo "Test 1"
deploy
echo "Test 2"
DEPLOY=$(knife ssh "$CREDS" "chef_environment:"${ENVIRONMENT}" AND roles:*"${ROLES}"*" "uname")
$DEPLOY
echo "Test 3"
knife ssh "$CREDS" "chef_environment:"$ENVIRONMENT" AND roles:*"$ROLES"*" "uname"
echo "Test 4"
knife ssh -x foobar -a ec2.local_ipv4 "chef_environment:dev01 AND roles:*tester*" "uname"
Again, Test 4 works only.
deploy() {instead offunction deploy {-- the former syntax works in any POSIX-compliant shell, whereas the latter depends on extensions and has no benefit to compensate for that lack of portability. And consider mklement0's note about all-uppercase variable names being in reserved space echoed.