variable-substitution

printf – store formatted string output in a variable

printf – store formatted string output in a variable Instead of the following printf ‘%st%st%st%sn’ ‘index’ ‘podcast’ ‘website’ ‘YouTube’ I want to store the printf output in a Results variable, how can I do this? Asked By: Porcupine || Source Bash (since 3.1), zsh (since 5.3) and ksh93 (since v- and u+m 2021-11-28) have an …

Total answers: 1

Output of loop variable shows different value than expected

Output of loop variable shows different value than expected Imagine I have two folders in my current working directory: back/ and front/. I am trying to enter inside each of them and do some stuff. From the simple script below: for dir in */ ; do echo "$(dir)" done I was expecting an output like: …

Total answers: 1

Execute copy commands from file

Execute copy commands from file I have a DE_CopyOldToNew.txt file with a whole bunch of copy commands for copying old file names to new file names. The file contains rows like : cp /migrationfiles/Company Name GmbH/2014.138_Old File Name.pdf /appl/data/docs/202403/DE_2014.138_NewFile_1.pdf cp /migrationfiles/Company Name GmbH/2014.139_Old File Name.pdf /appl/data/docs/202403/DE_2014.139_NewFile_1.pdf In my shell script I am iterating over each …

Total answers: 3

If-else Parameter substitution

If-else Parameter substitution I’m running a series of experiments with bash and want to store the log files in a directory whose name is based on the experiment configuration. Some items in the configuration are boolean (true/false). Take the following configuration as an example: batch_size=16 fp16=false bf16=true checkpoint_activations=true I’d like to store the log file …

Total answers: 3

bash var substitution inside json to use in curl

bash var substitution inside json to use in curl Team, I am unable to substitute values inside json(myjson) to use in curl command. any hint? JENKINS_USERNAME="svc-user" JENKINS_USER_TOKEN="xxxx" JENKINS_INSTANCE_FQDN="sham.blsm.comp.com" INSTANCE="test-verify" CREDENTIAL_ID_NAME="test" USERNAME_KEY="tests" USER_PASSWORD_KEY="testst" ID_DESCRIPTION="asdfasdfasdf" myjson=”{ "credentials": { "scope": "GLOBAL", "id": "’"${CREDENTIAL_ID_NAME}"’", "username": "’"${USERNAME_KEY}"’", "password": "’"${USER_PASSWORD_KEY}"’", "description": "’"${ID_DESCRIPTION}"’", "$class": "com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl" } }” curl -X POST https://${JENKINS_USERNAME}:${JENKINS_USER_TOKEN}@${JENKINS_INSTANCE_FQDN}/${JENKINS_INSTANCE}/credentials/store/system/domain/_/createCredentials –data-urlencode …

Total answers: 2

SSH's -t option – how do I remotely execute a complex command that includes pipes and variable expansion?

SSH's -t option – how do I remotely execute a complex command that includes pipes and variable expansion? The following script is run by root and is meant to share two directories from jim’s computer to that of oumaima. #!/usr/bin/bash au=$(echo "$(head -n 1 /etc/doas.conf)"|sed ‘s/permit//;s/as//;s/root//;s/ //g’) dirs=( "/var/lib/transmission-daemon/downloads" "/home/${au}/VisualArts/films_FR" ) dst_ip="[fe80::cc08:9467:8dba:15a9%wlp4s0]" her_host="fe80::cc08:9467:8dba:15a9%wlp4s0" give_oumaima() { …

Total answers: 1

How do I correctly define and interpolate this variable into my find command?

How do I correctly define and interpolate this variable into my find command? I wrote a Bash script, mscript.sh for managing my music files, a part of it creates a list of such files. all_exts=" -iname "*.webm" -o -iname "*.mkv" " #all_exts=’ -iname "*.webm" -o -iname "*.mp3" -o -iname "*.mp4" -o -iname "*.mkv" ‘ #The …

Total answers: 1

How to grep for the value of a variable inside a shell script?

How to grep for the value of a variable inside a shell script? I have 2 files: a file full of values I want to look for my source text file I wrote a short shell command to loop thru my list of values and grep against my source file. If it doesn’t find the …

Total answers: 2

${parameter:?word} expansion: `}` difference between bash and zsh

${parameter:?word} expansion: `}` difference between bash and zsh I’m trying to get a literal } in the output of the ${parameter:?word} expansion in zsh. In bash, it works as expected — I can quote the } with a : % bash $ echo "${unset_var_name?Here is a close brace >>}<<}" bash: unset_var_name: Here is a close …

Total answers: 1

ksh parameter expansion: replace / with –

ksh parameter expansion: replace / with – Say I have this: str="@test/string" echo $str @test/string echo ${str#@} test/string works as expected, but echo ${str////-} ksh: ${str////-}: bad substitution fails. (expected @test-string) What is a correct way to replace/substitute characters like this? echo $KSH_VERSION KSH version @(#)PD KSH v5.2.14 99/07/13.2 Asked By: Lee || Source Bound …

Total answers: 3

Shell script: Using variables makes command fails ( substituting values of variables manually ; command works fine )

Shell script: Using variables makes command fails ( substituting values of variables manually ; command works fine ) In a bash script: jenkins_folder=`cut -d "|" -f1 -s input.csv` jenkins_url=’https://url.com:8181/jenkins/view/’ echo "jenkins_folder : ${jenkins_folder}" for job in `java -jar jenkins-cli.jar -s ${jenkins_url}${jenkins_folder} list-jobs ${jenkins_folder} ` do echo "Job name:: ${job} ****" java -jar jenkins-cli.jar -s ${jenkins_url}${jenkins_folder} …

Total answers: 1

Bash variable substitution into yq command

Bash variable substitution into yq command Given a YAML file, example.yaml: node: sub_node: get_this: I’d like to get a variable containing get_this using Mike Farah’s yq and the string sub_node yaml="$(cat example.yaml)" nodename=sub_node sub_yaml= "$(echo "$yaml" | yq -r ‘.$nodename’ )"; # also tried -> sub_yaml= "$(echo "$yaml" | yq -r ‘.’"$nodename" )"; Note this …

Total answers: 2

curl not working with when a parameter is passed via a variable in shell script

curl not working with when a parameter is passed via a variable in shell script The following command works when entered directly on the console: [root@server user]# curl -d ‘parameter=170.22.16.14 192.10.2.24 – – [01/Jan/1900:00:00:00 -0300] "GET /files/notes.txt HTTP/1.1" 200 112061 "-" "Java/1.8.0.160"’ -H ‘Content-Type: application/x-www-form-urlencoded’ -X POST http://192.138.1.10:8080/system/add On the other side, I have a …

Total answers: 1

why doesn't A="" grep $A behave like grep ""?

why doesn't A="" grep $A behave like grep ""? I am on Linux, using bash. $ A="" $ grep $A nomatch "" "" <- (returned by grep) $ grep "" imatcheverything imatcheverything <- (returned by grep) I would have expected grep $A to act like grep "". Why does it literally match ""? I expect …

Total answers: 3

bash: different behaviour of script and in terminal (loop over files)

bash: different behaviour of script and in terminal (loop over files) I’m trying to loop over files with having different searching conditions based on the folder which I used with a case statement. basically it’s: #!/bin/bash case folder in "Testordner") search_filename=*_0_*.txt ;; "Ordner2") search_filename=*_65_*fg*.txt ;; *) ;; esac for files in $search_filename; do echo $files …

Total answers: 1

Export a value from input variable

Export a value from input variable I need to export the values from input. However it’s failing in bash. Here’s the code – echo "Please enter HOST :" read RDSHOST && export RDSHOST And my value is something like – myserver.abc-zone5.com I am getting this error when i run the script – unexpected EOF while …

Total answers: 2

Unable to use a variable to specify the targets for rsync's "–exclude={..}" option within script

Unable to use a variable to specify the targets for rsync's "–exclude={..}" option within script My goal is to have my bash script run this command: rsync -azhi –dry-run –exclude={‘file1.txt’,’file2.txt’,’*.sql’} /from-directory/ /to-directory/ … when abstracted thusly: srcdir=’/from-directory/’ dstdir=’/to-directory/’ excludes="{‘file1.txt’,’file2.txt’,’*.sql’}" rsync -azhi –dry-run –exclude="$excludes" "$srcdir" "$dstdir" $srcdir and $dstdir evaluate correctly, but $excludes is not being …

Total answers: 2