4
votes

When creating an inline bash command like this in Azure DevOps:

checksum="$(cksum file.txt)"

I'll wind up seeing cksum file.txt as a required parameter. For whatever reason, this behavior isn't consistent so sometimes I've setup build pipelines that work fine with inline Bash scripts, but inevitably, I'll run into this issue and be unable to fix it.

I even tried setting the cksum file.txt parameter to cksum file.txt, but replaces the space with an encoded string: %20. It becomes cksum%20file.txt which isn't a valid command in bash.

Here's the full script:

yarnCacheFilename="$(cksum yarn.lock).yarnCache.docker.tgz"

wget "https://example.azureedge.net/yarn-cache/$yarnCacheFilename"

if [ -f "$yarnCacheFilename" ]; then
    mkdir node_modules
    tar -xzvf "$yarnCacheFilename"
else
    yarn install --production
fi

Simple enough. That's code I can run in any bash terminal. Sadly, Azure DevOps is adding a parameter to the Task Group:

Azure DevOps adding parameter for inline <code>bash</code> command

The fact that this parameter exists means Azure DevOps is preventing my bash file from executing properly and string-replacing the most-crucial part.

How do I work around this issue?

2

2 Answers

7
votes

You can use command substitution so replace the $() with a back tick ` at both sides.

https://www.gnu.org/software/bash/manual/html_node/Command-Substitution.html for more information about command substitution.

checksum="`cksum file.txt`"

Full Script Example

yarnCacheFilename="`cksum yarn.lock`.yarnCache.docker.tgz"

wget "https://example.azureedge.net/yarn-cache/$yarnCacheFilename"

if [ -f "$yarnCacheFilename" ]; then
    mkdir node_modules
    tar -xzvf "$yarnCacheFilename"
else
    yarn install --production
fi
1
votes

If you mean you want to pass the string "cksum file.txt" as a parameter in inline bash script, then you can set a variable and use the variable in subsequent tasks/scripts.

For example:

  1. Set the checksum variable:

    echo "##vso[task.setvariable variable=checksum]cksum file.txt"
    
  2. Read the variables

    echo "No problem reading $CHECKSUM"
    

Please see Define and modify your variables in a script for details.

If you run a script specified with a path something like $/project/script/Test0427/bash.sh, then you can directly pass the string as a parameter:

  1. Arguments:

    "cksum file.txt" "Test Arguments 1018"

  2. Read the variables

    echo "No problem reading $1 and $2"
    

enter image description here