Bash pass variable name as argument - bash

I have a simple script (let's call it script.sh):
#!/bin/bash
source variables
echo $1
What I want to achieve is scripts output:
255.255.255.255
When executed like:
./script.sh VARIABLE
where VARIABLE is defined in variables file:
VARIABLE=255.255.255.255
Is it possible?

example:
#!/bin/bash
variable=255.255.255.255
param=$1
echo ${!param}
then execute:
bash script.sh variable
255.255.255.255
The exclamation mark makes param to get the value of the variable with that name. See about parameter indirection.

Could you please try following it may help you in same.
./script.sh "$VARIABLE"

You have to add $ before variable name. For example:
./script.sh $VARIABLE
Is good to put argument in double quotes like:
./script.sh „$VARIABLE”
Writing from mobile phone, sorry for no using syntax.

Related

How to replace bash variable with command line arguments

My bash script bash.sh only contains one line
echo "${abc:=123}"
I learned that := is used to assign default values. So when I run bash.sh abc=abc, I expect the output to be abc.
However, the output is still 123.
Why is that? Am I call the script in the wrong way? Thanks.
Bash positional arguments are set to $1, $2, etc. Change your script to:
abc=$1
echo "${abc:=123}"
this will make it so if the variable abc is unset the default value is echoed but if another value is passed on the command line abc will be set to that value.
You are passing a parameter and expecting to see it in an environment variable.
If you want to set an environment variable, you can do that before the script name:
$ cat foo
#!/bin/bash
echo "${abc:=123}"
$ ./foo
123
$ abc=hello ./foo
hello

Dynamically make the variable and get the corresponding value in shell script

I have written the shell script which gets the variables from other script(def.sh)
My def.sh looks like below:
SERVERS[0]=host1.com
SERVERS[1]=host1.com
SERVER1_HOME_DIR="/path1"
SERVER2_HOME_DIR="/path2"
My main.sh will try to use these variables from def.sh using for loop:
count=1
for server in ${SERVERS[#]}
do
echo "value " $(echo server${count}_HOME_DIR) // I'm not getting the /path1 here
count=`expr $count + 1`
done
Hi,
It will still printing the value SERVER1_HOME_DIR & SERVER2_HOME_DIR
Please help to get the value of the server home directories.
Thanks in Advance.
You can build the variable name and store it in a variable and use that. When doing variable expansion in braces, if the first char is ! the rest can be the name of the variable that will be expanded and be the name of the variable for the "outer" expansion. Try it like so:
name=SERVER${count}_HOME_DIR
echo ${!name}

Pass variable value as argument from one script to another shell script, as an argument

I have two shell scripts, one is my own script and another is a 3rd party stuff.
I take a path-to-file as argument in script1.
Script2 is a 3rd party script which takes path-to-file as an argument. I do not wish to modify this script.
I am doing something like this, in script1,
a=$1
./script2 $a
But the $a is being passed as a string.. as in, '$a' is being passed and not the actual value of variable a :(
Is there a way in which I can achieve this? Please help!
my_script.sh
=======================================
#!/bin/bash
1st="sam"
2nd="victor"
3rd="cris"
/home/admin/Third_party.sh "$1st" "$2nd" "$3rd"
Third_party.sh
=======================================
#!/bin/bash
echo "1st value is: $1 ....."
echo "2nd value is: $2 ....."
echo "3rd value is: $3 ....."
You should use single quote to pass variable . like '$1st' in your case.

How to quote parameters when calling a shell script from within another script using a variable

I have two shell scripts. One script dynamically creates the call to the second script, based on the parameter it received, and then executes the call.
My problem is that the parameters the first script gets may contain spaces, so I must quote the parameter in the call to script2.
This is an example to the problem:
script1.sh:
#!/bin/sh
param=$1
command="./script2.sh \"$param\""
echo $command
$command
script2.sh:
#!/bin/sh
param=$1
echo "the value of param is $param"
When I run:
./script1.sh "value with spaces"
I get:
./script2.sh "value with spaces"
the value of param is "value
Which is of course not what I need.
What is wrong here??
TIA.
EDIT :
I found the solution thanks to the useful link in tripleee's comment. Here it is in case it helps anybody.
In short, in order to solve this, one should use an array for the arguments.
script1.sh:
#!/bin/sh
param=$1
args=("$param")
script_name="./script2.sh"
echo $script_name "${args[#]}"
$script_name "${args[#]}"
Use "$#" to refer to all command-line parameters with quoting intact.

setting variable with unknown name in bash

I would like to set a variable which name is stored in a file (is an output of a sed executed earlier)
the file would look like:
py1
so setting our variable would be like: set cat file=value
but echoing $py1 gives me nothing.
Is that possible with bash version 2.05?
This is the "preferred" way to do it (bash) without using cat or eval
declare $(<file)=value
Use eval:
eval "$(cat file)=value"
Update: The command substitution $(cat file) can be replaced by the equivalent but faster $(< file).
You'll need to use eval
eval $(cat file)=value

Resources