Sample Header Ad - 728x90

Unix & Linux Stack Exchange

Q&A for users of Linux, FreeBSD and other Unix-like operating systems

Latest Questions

0 votes
4 answers
1869 views
create variables from CSV with varying number of fields
Looking for some help turning a CSV into variables. I tried using IFS, but seems you need to define the number of fields. I need something that can handle varying number of fields. *I am modifying my original question with the current code I'm using (taken from the answer provided by hschou) which i...
Looking for some help turning a CSV into variables. I tried using IFS, but seems you need to define the number of fields. I need something that can handle varying number of fields. *I am modifying my original question with the current code I'm using (taken from the answer provided by hschou) which includes updated variable names using type instead of row, section etc. I'm sure you can tell by my code, but I am pretty green with scripting, so I am looking for help to determine if and how I should add another loop or take a different approach to parsing the typeC data because although they follow the same format, there is only one entry for each of the typeA and typeB data, and there can be between 1-15 entries for the typeC data. The goal being only 3 files, one for each of the data types. Data format: Container: PL[1-100] TypeA: [1-20].[1-100].[1-1000].[1-100]-[1-100] TypeB: [1-20].[1-100].[1-1000].[1-100]-[1-100] TypeC (1 to 15 entries): [1-20].[1-100].[1-1000].[1-100]-[1-100] *There is no header in the CSV, but if there were it would look like this (Container, typeA, and typeB data always being in position 1,2,3, and typeC data being all that follow): Container,typeA,typeB,typeC,tycpeC,typeC,typeC,typeC,.. CSV: PL3,12.1.4.5-77,13.6.4.5-20,17.3.577.9-29,17.3.779.12-33,17.3.802.12-60,17.3.917.12-45,17.3.956.12-63,17.3.993.12-42 PL4,12.1.4.5-78,13.6.4.5-21,17.3.577.9-30,17.3.779.12-34 PL5,12.1.4.5-79,13.6.4.5-22,17.3.577.9-31,17.3.779.12-35,17.3.802.12-62,17.3.917.12-47 PL6,12.1.4.5-80,13.6.4.5-23,17.3.577.9-32,17.3.779.12-36,17.3.802.12-63,17.3.917.12-48,17.3.956.12-66 PL7,12.1.4.5-81,13.6.4.5-24,17.3.577.9-33,17.3.779.12-37,17.3.802.12-64,17.3.917.12-49,17.3.956.12-67,17.3.993.12-46 PL8,12.1.4.5-82,13.6.4.5-25,17.3.577.9-34 Code: #!/bin/bash #Set input file _input="input.csv" # Pull variables in from csv # read file using while loop while read; do declare -a COL=( ${REPLY//,/ } ) echo -e "containerID=${COL}\ntypeA=${COL}\ntypeB=${COL}" >/tmp/typelist.txt idx=1 while [ $idx -lt 10 ]; do echo "typeC$idx=${COL[$((idx+2))]}" >>/tmp/typelist.txt let idx=idx+1 #whack off empty variables sed '/\=$/d' /tmp/typelist.txt > /tmp/typelist2.txt && mv /tmp/typelist2.txt /tmp/typelist.txt #set variables from temp file . /tmp/typelist.txt done sleep 1 #Parse data in this loop.# echo -e "\n" echo "Begin Processing for $container" #echo $typeA #echo $typeB #echo $typeC #echo -e "\n" #Strip - from sub data for extra parsing typeAsub="$(echo "$typeA" | sed 's/\-.*$//')" typeBsub="$(echo "$typeB" | sed 's/\-.*$//')" typeCsub1="$(echo "$typeC1" | sed 's/\-.*$//')" #strip out first two decimils for extra parsing typeAprefix="$(echo "$typeA" | cut -d "." -f1-2)" typeBprefix="$(echo "$typeB" | cut -d "." -f1-2)" typeCprefix1="$(echo "$typeC1" | cut -d "." -f1-2)" #echo $typeAsub #echo $typeBsub #echo $typeCsub1 #echo -e "\n" #echo $typeAprefix #echo $typeBprefix #echo $typeCprefix1 #echo -e "\n" echo "Getting typeA dataset for $typeA" #call api script to pull data ; echo out for test echo "API-gather -option -b "$typeAsub" -g all > "$container"typeA-dataset" sleep 1 echo "Getting typeB dataset for $typeB" #call api script to pull data ; echo out for test echo "API-gather -option -b "$typeBsub" -g all > "$container"typeB-dataset" sleep 1 echo "Getting typeC dataset for $typeC1" #call api script to pull data ; echo out for test echo "API-gather -option -b "$typeCsub" -g all > "$container"typeC-dataset" sleep 1 echo "Getting additional typeC datasets for $typeC2-15" #call api script to pull data ; echo out for test echo "API-gather -option -b "$typeCsub2-15" -g all >> "$container"typeC-dataset" sleep 1 echo -e "\n" done < "$_input" exit 0 Speed isnt a concern, but if I've done anything really stupid up there, feel free to slap me in the right direction. :)
Jdubyas (45 rep)
Jul 12, 2017, 05:09 AM • Last activity: Aug 6, 2025, 12:04 AM
1 votes
3 answers
1829 views
Putting the contents of /dev/urandom into a variable?
This is a follow on from my previous thread :- https://unix.stackexchange.com/questions/651412/passing-date-command-into-a-variable Ok so effectively I would like /dev/urandom to generate 4 digits for me. And place them into a variable. Here is an example of my bash script :- ``` #!/bin/bash for ((...
This is a follow on from my previous thread :- https://unix.stackexchange.com/questions/651412/passing-date-command-into-a-variable Ok so effectively I would like /dev/urandom to generate 4 digits for me. And place them into a variable. Here is an example of my bash script :-
#!/bin/bash
for (( c=0; c<=10; c++))
do
        a="$(tr -dc '[:digit:]' < /dev/urandom | fold -w 4)"

        echo $a
done
The only reason I wish to "Echo" the results is so I can actually see that /dev/urandom has generated 4 digits for me correctly. For my bash script I wish to make, I need to do this programmatically as I wish to do further operation's on the generated results. So I need not "Echo" the results. so is a="$(tr -dc '[:digit:]' < /dev/urandom | fold -w 4)" Actually placing 4 random digits into the variable "a"? when I Echo "a" from my script I just get a hanging console and have to use "CTRL+C" to bring my console back. All I am trying do is place 4 random digits from /dev/urandom into a variable. I m a new to bash scripting so all advice welcome and thank you in advanced for any help or suggestions.
Shanzem (35 rep)
Jun 22, 2021, 09:02 AM • Last activity: Jul 16, 2025, 02:32 AM
1 votes
1 answers
2662 views
How can I use a variable in awk command
With my code I am trying to sum up the values with the specific name of a column in a csv file, depending on the input of the name. Here's my code: ``` #!/bin/bash updatedata() { index=0 while IFS="" read -r line do IFS=';' read -ra array <<< "$line" for arrpos in "${array[@]}" do if [ "$arrpos" ==...
With my code I am trying to sum up the values with the specific name of a column in a csv file, depending on the input of the name. Here's my code:
#!/bin/bash

updatedata() {

    index=0
    while IFS="" read -r line
    do
        IFS=';' read -ra array <<< "$line"
        for arrpos in "${array[@]}"
        do
            if [ "$arrpos" == *"$1"* ] || [ "$1" == "$arrpos" ]
            then
                break
            else
                let index=index+1
            fi
        done
        break
       
    done < data.csv
    ((index=$index+1))


       
    if [ $pos -eq 0 ]
    then
        v0=$(awk -F";", -v index=$index '{x+=$index}END{print x}' ./data.csv )
    elif [ $pos -eq 1 ]
    then
        v1=$(awk -F";" '{x+=$index}END{print x}' ./data.csv )
    elif [ $pos -eq 2 ]
    then
        v2=$(awk -F";" '{x+=$index}END{print x}' ./data.csv )
    elif [ $pos -eq 3 ]
    then
        v3=$(awk -F";" '{x+=$index}END{print x}' ./data.csv )
    fi
               
                   
         
}
` In the middle of the code you can see in v0=, I was trying to experiment a little, but I just keep getting errors: First I tried this:
v0=$(awk -F";" '{x+=$index}END{print x}' ./data.csv)
but it gave me this error: 'awk: line 1: syntax error at or near }' so then I decided to try this(as you can see in the code)
v0=$(awk -F";", -v index=$index '{x+=$index}END{print x}' ./data.csv )
And I got this error: 'awk: run time error: cannot command line assign to index type clash or keyword FILENAME="" FNR=0 NR=0' I don't know what to do. Can you guys help me.
anonymous (11 rep)
Aug 28, 2020, 09:26 AM • Last activity: Jun 25, 2025, 10:52 AM
12 votes
4 answers
20267 views
How to redirect stderr in a variable but keep stdout in the console
My goal is to call a command, get stderr in a variable, but keep stdout (and only stdout) on the screen. Yep, that's the opposite of what most people do :) For the moment, the best I have is : #!/bin/bash pull=$(sudo ./pull "${TAG}" 2>&1) pull_code=$? if [[ ! "${pull_code}" -eq 0 ]]; then error "[${...
My goal is to call a command, get stderr in a variable, but keep stdout (and only stdout) on the screen. Yep, that's the opposite of what most people do :) For the moment, the best I have is : #!/bin/bash pull=$(sudo ./pull "${TAG}" 2>&1) pull_code=$? if [[ ! "${pull_code}" -eq 0 ]]; then error "[${pull_code}] ${pull}" exit "${E_PULL_FAILED}" fi echo "${pull}" But this can only show the stdout in case of success, and after the command finish. I want to have stdout on live, is this possible ? **EDIT** Thanks to @sebasth, and with the help of https://unix.stackexchange.com/questions/430161/redirect-stderr-and-stdout-to-different-variables-without-temporary-files , I write this : #!/bin/bash { sudo ./pull "${TAG}" 2> /dev/fd/3 pull_code=$? if [[ ! "${pull_code}" -eq 0 ]]; then echo "[${pull_code}] $(cat<&3)" exit "${E_PULL_FAILED}" fi } 3<
Doubidou (223 rep)
Oct 9, 2018, 08:46 AM • Last activity: Jun 10, 2025, 06:30 PM
0 votes
1 answers
47 views
bash conditional variable assignment cli not what I expected
Can anyone explain what's going on with DR here? ``` user@host:~# DRYRUN=true user@host:~# echo "$DRYRUN" true user@host:~# $DRYRUN && export DR="-n" user@host:~# echo $DR user@host:~# $DRYRUN && export DR="-n" && echo $DR user@host:~# $DRYRUN && export DR="-n" && echo "$DRYRUN --- $DR" true --- -n...
Can anyone explain what's going on with DR here?
user@host:~# DRYRUN=true
user@host:~# echo "$DRYRUN"
true
user@host:~# $DRYRUN && export DR="-n"
user@host:~# echo $DR
user@host:~# $DRYRUN && export DR="-n" && echo $DR
user@host:~# $DRYRUN && export DR="-n" && echo "$DRYRUN --- $DR"
true --- -n
user@host:~# echo $DR
user@host:~# echo "$DRYRUN --- $DR"
true --- -n
user@host:~#
I Just want to assign DR to the string "-n" when DRYRUN is set to true but what I'm getting is confusing
Hugh (19 rep)
May 12, 2025, 03:04 PM • Last activity: May 12, 2025, 03:43 PM
0 votes
0 answers
38 views
bash subshell execution behaves unexpectedly
I have a script which is supposed to fetch 2 URLs sequentially: #!/bin/bash wget_command='wget --restrict-file-names=unix https://www.example.com/{path1,path2}/' $($wget_command) echo $wget_command >> wget_commands.log results in this message: --2025-04-30 09:13:49-- https://www.example.com/%7Bpath1...
I have a script which is supposed to fetch 2 URLs sequentially: #!/bin/bash wget_command='wget --restrict-file-names=unix https://www.example.com/{path1,path2}/ ' $($wget_command) echo $wget_command >> wget_commands.log results in this message: --2025-04-30 09:13:49-- https://www.example.com/%7Bpath1,path2%7D/ ^ the %7D is a problem ---------- Whereas, directly issuing: wget --restrict-file-names=unix https://www.example.com/{path1,path2}/ results in the expected fetches and messages: --2025-04-30 09:14:13-- https://www.example.com/path1/ ... --2025-04-30 09:14:14-- https://www.example.com/path2/ ---------- It feels like {path1,path2} is triggering an expansion/interpolation when $($wget_command) goes to use it but I am not sure how to verify nor avoid this problem.
MonkeyZeus (143 rep)
Apr 30, 2025, 01:30 PM
6 votes
3 answers
24277 views
Get the output from expect script in a variable
I have an expect script which provides the IP address: #!/bin/expect -f set nodename [lindex $argv 0] spawn virsh console $nodename expect "Escape character is" send "\n" expect "localhost login: " { send "root\n" expect "Password: " send "cloud123\n" } expect "~]#" { send "\n" send "ifconfig | grep...
I have an expect script which provides the IP address: #!/bin/expect -f set nodename [lindex $argv 0] spawn virsh console $nodename expect "Escape character is" send "\n" expect "localhost login: " { send "root\n" expect "Password: " send "cloud123\n" } expect "~]#" { send "\n" send "ifconfig | grep 192.168.1. | awk \'{print \$2}\'" send "\n" expect '(\d+\.\d+\.\d+\.\d+)' send "logout" } I want the script to return this IP address. I am calling this expect script from a shell script as below #!/bin/bash ip=$(expect GetIP.exp nodetwo) echo $ip How can I make my expect script return output to the shell script?
karan ratnaparkhi (111 rep)
Oct 28, 2015, 08:47 AM • Last activity: Mar 18, 2025, 03:22 PM
0 votes
2 answers
134 views
How to apply an ls to a file name with spaces in a variable
This code my_file="/tmp/file_without_spaces" if [ -f "${my_file}" ]; then my_ls_aaaammgg_hhss="$(ls ${my_file} -l --time-style='+%Y%m%d_%H%M%S' | cut -d' ' -f6)" mv "${my_file}" "${my_file}_${my_ls_aaaammgg_hhss}" fi changes `/tmp/file_without_spaces` into `/tmp/file_without_spaces_aaaammgg_hhss`, f...
This code my_file="/tmp/file_without_spaces" if [ -f "${my_file}" ]; then my_ls_aaaammgg_hhss="$(ls ${my_file} -l --time-style='+%Y%m%d_%H%M%S' | cut -d' ' -f6)" mv "${my_file}" "${my_file}_${my_ls_aaaammgg_hhss}" fi changes /tmp/file_without_spaces into /tmp/file_without_spaces_aaaammgg_hhss, for example /tmp/file_without_spaces_20250218_161244. How can I do the same thing with a file with spaces in its name, for example /tmp/file with spaces ? I would like to get /tmp/file with spaces_20250218_161244.
CarLaTeX (331 rep)
Feb 18, 2025, 03:22 PM • Last activity: Feb 18, 2025, 07:21 PM
-1 votes
2 answers
73 views
set output into variable from grep from input variable
whats wrong with this bash script: acme2=$(dig txt @$1 _acme-challenge.$1.de) acme3=$(echo $acme2 | grep "^_acme") acme2 has the whole output, but acme3 is always empty I searched several solutions, and tried other possibilities, but nothing works... acme3=echo $acme2|grep acme acme3=$($acme2|grep "...
whats wrong with this bash script: acme2=$(dig txt @$1 _acme-challenge.$1.de) acme3=$(echo $acme2 | grep "^_acme") acme2 has the whole output, but acme3 is always empty I searched several solutions, and tried other possibilities, but nothing works... acme3=echo $acme2|grep acme acme3=$($acme2|grep "^_acme") acme3=$(grep "acme" $acme2) acme3=$(echo "$acme2" | grep "^_acme")
dg1kpc (1 rep)
Feb 12, 2025, 03:29 PM • Last activity: Feb 18, 2025, 07:59 AM
-3 votes
1 answers
78 views
Need working examples of using tilde expansion immediately following the : (colon) sign in variable assignment
In the Bash manual, it is written about the tilde expansion: > Each variable assignment is checked for unquoted tilde-prefixes > immediately following a ‘:’ or the first ‘=’. [Read the Bash manual about tilde expansion.][1] [1]: https://www.gnu.org/software/bash/manual/html_node/Tilde-Expansion.html...
In the Bash manual, it is written about the tilde expansion: > Each variable assignment is checked for unquoted tilde-prefixes > immediately following a ‘:’ or the first ‘=’. Read the Bash manual about tilde expansion. I assumed that the : sign when assigning a variable is related to parameter expansion and instructed one of the popular AIs to find examples that satisfy the conditions defined in the Bash manual. The summary of his response is as follows: - There isn't a valid working example with the tilde immediately following the : without additional characters.
Kiki Miki (27 rep)
Feb 12, 2025, 12:07 PM • Last activity: Feb 13, 2025, 04:15 AM
14 votes
3 answers
4963 views
Use a shell variable in awk
Here is my script (to find the files that contain a specified pattern): find . -type f \ -exec awk -v vawk="$1" '/'"$vawk"'/ {c++} c>0 { print ARGV[1]; exit 0 } END { if (! c) {exit 1}}' \{\} \; I would like to use my script with an argument &#167;: MyScript.sh pattern My problem is that I don't man...
Here is my script (to find the files that contain a specified pattern): find . -type f \ -exec awk -v vawk="$1" '/'"$vawk"'/ {c++} c>0 { print ARGV; exit 0 } END { if (! c) {exit 1}}' \{\} \; I would like to use my script with an argument §: MyScript.sh pattern My problem is that I don't manage to put the $1 variable in awk. When I try to debug my script bash -x MyScript.sh pattern Here is the output : + find . -type f -exec awk -v vawk=pattern '// {c++} c>0 {print ARGV ; exit 0 } END { if (! c) {exit 1}}' '{}' ';' The $vawk variable seems to be empty. Any idea?
Nicolas (419 rep)
Oct 5, 2012, 06:26 PM • Last activity: Feb 11, 2025, 07:47 PM
1 votes
1 answers
71 views
Zsh’s autocompletion options from variable
## General overview I have to set a values auto-completion in zsh for a command (in the following minimal example, I will show it with `testcmd`). So my current code work quiet well with hardcoded values: #### Current auto-completion code (the one who works) ```zsh function testcmd() { echo "Nothing...
## General overview I have to set a values auto-completion in zsh for a command (in the following minimal example, I will show it with testcmd). So my current code work quiet well with hardcoded values: #### Current auto-completion code (the one who works)
function testcmd()
{
	echo "Nothing to do, just a test command"
}


_test_complete() {
	_values \
		"Possible values" \
		foo'[Foo]' \
		bar'[Bar baz]' \
}

compdef _test_complete testcmd
#### Current auto-completion behavior When I type testcmd , I correctly get the following desired rendering:
$ testcmd
Possible values
bar  -- Bar baz
foo  -- Foo
## What is the goal I search to reach But as you see, the values are hard-coded inside the function, ideally, the function should retrieve them from a variable. ## What I already try So naturaly, I put the values inside the variable values_variable as following: #### The tried code (the one who doesn’t works)
function testcmd()
{
	echo "Nothing to do, just a test command"
}


_test_complete() {
	local values_variable
	values_variable="foo'[Foo]' \
		bar'[Bar baz]' \ "
	_values \
		"Possible values" \
		${values_variable}
}

compdef _test_complete testcmd
#### The behavior of the tried code But then, when I try testcmd it completely fail:
$ testcmd
_values:compvalues:11: invalid value definition: foo'[Foo]' \t\tbar'[Bar baz]' \
_values:compvalues:11: invalid value definition: foo'[Foo]' \t\tbar'[Bar baz]' \
_values:compvalues:11: invalid value definition: foo'[Foo]' \t\tbar'[Bar baz]' \
$ testcmd
### What I _also_ did - I tried to escape the spaces with echo ${values_variable} | sed "s/ /\\ /g" ; - I tried to use $values_variable with the eval command to pretend as if it content was directly typed inside the _values definition ; - I tried both eval and escaping with eval $(echo ${values_variable} | sed "s/ /\\ /g") ; - I tried to eveal line by line with a loop :
echo "$values_variable" | while read -r line; do
    eval "$line"
  done
- Expansion with ${(@f)values_variable}̀ ; - Many other ideas. But it also failed. ### The nearest solution I found In [_How to pass the contents of a file using cat to _values (zsh completion)_](https://stackoverflow.com/questions/21356370/how-to-pass-the-contents-of-a-file-using-cat-to-values-zsh-completion) tread I found a solution for values imported from an external file, but the user seems to be fronted to the same space escape problem. However, I can’t adjust it to the case with the $values_variables internal variable. I naturally tried this one, who doesn’t works either:
_test_complete() {
	local values_variable
	values_variable="foo'[Foo]' \
		bar'[Bar baz]' \ "
	OLD_IFS=$IFS
	IFS=$'\n'
	_values \
		"Possible values" \
		${values_variable}
	IFS=$OLD_IFS
}
## The question How can I load the values to give to _values inside auto-completion function from a variable?
fauve (1529 rep)
Feb 2, 2025, 05:54 PM • Last activity: Feb 3, 2025, 07:10 PM
1 votes
3 answers
99 views
Bash/macOS: Getting a variable by its name, inside of a function
In Bash for macOS, I need to be able to pass a string to a function and return the value of a variable named after that string, as well as the string itself. Here's what I have so far, which doesn't work: ``` varTest="If you can read this, it worked" function testFunction { echo "Variable name is $1...
In Bash for macOS, I need to be able to pass a string to a function and return the value of a variable named after that string, as well as the string itself. Here's what I have so far, which doesn't work:
varTest="If you can read this, it worked"

function testFunction {
    echo "Variable name is $1"
    echo "Contents are $(${$1})"
}

testFunction varTest
> Variable name is varTest
> Contents are varTest
When actually the second line should say Contents are If you can read this, it worked. In PowerShell it'd be easy:
$varTest="If you can read this, it works"

function testEcho ($string) {
   write-host "Variable name is $string"
   (get-variable $string).value
}

testEcho varTest
> Variable name is varTest
> If you can read this, it works
Does bash have any equivalent to Get-Variable? This will be for Bash (not ZSH) on macOS devices (so v3.2.57).
seagull (111 rep)
Feb 3, 2025, 04:23 PM • Last activity: Feb 3, 2025, 06:05 PM
-1 votes
3 answers
19253 views
How to assign a string with multiple spaces to a variable in bash?
First its not like this question [How do I echo a string with multiple spaces in bash “untouched”? \[duplicate\]][1] because in that question he just want to print it and I want to assign it to variable and save it. I've tried this: SPACE=' ' VAR="$VAR1${SPACE}$VAR2" [1]: https://unix.stackexchange....
First its not like this question [How do I echo a string with multiple spaces in bash “untouched”? \[duplicate\]][1] because in that question he just want to print it and I want to assign it to variable and save it. I've tried this: SPACE=' ' VAR="$VAR1${SPACE}$VAR2"
Wissam Roujoulah (3267 rep)
Dec 3, 2016, 03:20 PM • Last activity: Jan 28, 2025, 10:27 AM
1 votes
1 answers
47 views
How To Use Quotes In Variable In Bash
I need to assign a command to a variable and execute it with variable in linux bash. The command executes fine from command line but not from the variable because of multiple quotes. I hope backslash should be used for multiple quotes to avoid the error. Please help with the correct format. **Code:*...
I need to assign a command to a variable and execute it with variable in linux bash. The command executes fine from command line but not from the variable because of multiple quotes. I hope backslash should be used for multiple quotes to avoid the error. Please help with the correct format. **Code:** parm="cat file|awk '$1>0 && $1="abc" {print f $0} {f=$0 ORS}'" eval "$parm" **Contents:** abc 123 efg 456 **Error:** awk: cmd. line:1: >0 && =abc {print f bash} {f=bash ORS} awk: cmd. line:1: ^ syntax error Thanks in advance!
Kishan (113 rep)
Jan 16, 2025, 05:05 AM • Last activity: Jan 16, 2025, 08:05 AM
21 votes
2 answers
12868 views
How Can I Expand A Tilde ~ As Part Of A Variable?
When I open up a bash prompt and type: $ set -o xtrace $ x='~/someDirectory' + x='~/someDirectory' $ echo $x + echo '~/someDirectory' ~/someDirectory I was hoping that the 5th line above would have went `+ echo /home/myUsername/someDirectory`. Is there a way to do this? In my original Bash script, t...
When I open up a bash prompt and type: $ set -o xtrace $ x='~/someDirectory' + x='~/someDirectory' $ echo $x + echo '~/someDirectory' ~/someDirectory I was hoping that the 5th line above would have went + echo /home/myUsername/someDirectory. Is there a way to do this? In my original Bash script, the variable x is actually being populated from data from an input file, via a loop like this: while IFS= read line do params=($line) echo ${params} done <"./someInputFile.txt" Still, I'm getting a similar result, with the echo '~/someDirectory' instead of echo /home/myUsername/someDirectory.
Andrew (312 rep)
Oct 20, 2017, 06:34 PM • Last activity: Dec 26, 2024, 01:34 AM
5 votes
4 answers
544 views
Getting multiple variables from the output of docker exec command in a bash script?
I would like to execute a script on a host machine (`script_on_host.sh`), which then reaches inside a docker container in order to grab some data using a second script `(script_in_container.sh`). The data is only available inside the container, and not from the host directly, hence the reason for do...
I would like to execute a script on a host machine (script_on_host.sh), which then reaches inside a docker container in order to grab some data using a second script (script_in_container.sh). The data is only available inside the container, and not from the host directly, hence the reason for doing it this way. I am having trouble passing data between the two scripts. I tried the following: **script_in_container.sh**
#!/bin/bash

x=1
y=2
z=6.3
echo $x
echo $y
echo $z
**script_on_host.sh**
#!/bin/bash

results=$(docker exec mycontainer './script_in_container.sh')
X=$results(1)
Y=$results(2)
Z=$results(3)
But it throws an error. Can someone help me with this? Is there a nice or more standard way to go about passing variables in this way? I feel like I'm maybe doing something a little clumsily here, by using echo to print multiple lines from the container script.
teeeeee (305 rep)
Dec 23, 2024, 10:01 PM • Last activity: Dec 24, 2024, 07:15 PM
3 votes
1 answers
174 views
Write the contents of a .conf file into a variable located in a separate .conf file
I am configuring my hyprland.conf file and I am attempting to do it in a clean and modular fashion. I am trying for a modular fashion so that I can share my dotfiles and someone else can change which components they are using without directly modifying the hyprland.conf file. Near the top of my hypr...
I am configuring my hyprland.conf file and I am attempting to do it in a clean and modular fashion. I am trying for a modular fashion so that I can share my dotfiles and someone else can change which components they are using without directly modifying the hyprland.conf file. Near the top of my hyprland.conf file I would like to create variables for use in other parts of the file, such as the keybindings. If I explicitly set the variables, such as $terminal = kitty, they work fine. However, when I attempt to populate the variable with the contents of a separate .conf file, it doesn't seem to populate. The terminal.conf file that I am attempting to retrieve the contents of contains only the following: kitty The code I am using to populate the variable in my hyprland.conf file is as follows: exec-once = export TERMINAL="$(cat ~/.config/hypr/settings/terminal.conf)" If I open a terminal and enter echo "$(cat ~/.config/hypr/settings/terminal.conf)" I get kitty as a response, which is what I am expecting to see in the variable. For the keybinding section of my hyprland.conf file, I am attempting to use the variable as follows: bind = SUPER, T, exec, "$TERMINAL" However, when I use that keybind to open a terminal, nothing happens. Therefore, I attempted to check the contents of the $TERMINAL variable in my hyprland.conf file by executing the following command: exec = echo "$TERMINAL" >> ~/.config/hypr/hyprland.conf All it adds to the file are empty lines. What am I doing wrong?
PrismaPixel Studios (221 rep)
Dec 11, 2024, 07:43 PM • Last activity: Dec 11, 2024, 08:07 PM
-1 votes
1 answers
110 views
Bash local nameref declaration does not work
I am trying to set a local variable in a function with nameref. The script code is the following: #!/usr/bin/bash msg=hello myparam='' superfunc () { productfile=$1 local -n refmyparam=$2 } superfunc $msg $myparam echo $myparam When running it I get the error: line 7: local: `': not a valid identifi...
I am trying to set a local variable in a function with nameref. The script code is the following: #!/usr/bin/bash msg=hello myparam='' superfunc () { productfile=$1 local -n refmyparam=$2 } superfunc $msg $myparam echo $myparam When running it I get the error: line 7: local: `': not a valid identifier We use **GNU bash, version 5.2.21**
trikelef (460 rep)
Dec 3, 2024, 11:29 AM • Last activity: Dec 4, 2024, 08:12 AM
1 votes
1 answers
79 views
How can you echo a variable name made of variable names I.e $$var1$var2
In this test I'm expecting it to print "var1 is 999". user@penguin:~$ for num in {1..3}; do export var$num=9999 ; echo var$num is $var$num ; done var1 is 1 var2 is 2 var3 is 3 user@penguin:~$ echo $var1 $var2 $var3 9999 9999 9999 This prints the PID instead of the variabled named by the two variable...
In this test I'm expecting it to print "var1 is 999". user@penguin:~$ for num in {1..3}; do export var$num=9999 ; echo var$num is $var$num ; done var1 is 1 var2 is 2 var3 is 3 user@penguin:~$ echo $var1 $var2 $var3 9999 9999 9999 This prints the PID instead of the variabled named by the two variable names. user@penguin:~$ for num in {1..3}; do export var$num=9999 ; echo var$num is $$var$num ; done var1 is 316var1 var2 is 316var2 var3 is 316var3
Dan Larrabee (21 rep)
Nov 13, 2024, 12:47 AM • Last activity: Nov 14, 2024, 03:29 PM
Showing page 1 of 20 total questions