How to replace bash variable with command line arguments - bash

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

Related

set a shell variable in make to use later on the same line

if you do this in sh:
ABC=123 && echo value is ${ABC}
you get this
value is 123
and if you put this in a makefile:
main:
echo value is ${ABC}
(except don't copy paste that because my tab is actually four spaces)
and then pass it into the file like this:
$ ABC=123 make main
echo value is 123
value is 123
everything works as expected
but I would like to set the sh variable on the same line inside the makefile
main:
ABC=123 && echo value is ${ABC}
and I get this:
$ make main
ABC=123 && echo value is
value is
How can I set a shell variable?
I am trying to do this because I am sourcing a file.. this is just a minimal example of thing thing that I thought would work that doesn't.
That's because the variable is expanded by make not shell. You can tell it by the very first line that prints echo without ${ABC}. You need to quote the dollar sign if you want to pass it to shell, e.g.:
$ cat Makefile
main:
ABC=123 && echo value is $${ABC}
$ make main
ABC=123 && echo value is ${ABC}
value is 123

Bash pass variable name as argument

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.

How to find a parameter value using input in shell script

I have a scenario to create a generic script that will use the input value and get actual value from config file and use it for further logic.
pattern.config file
TYPE1_PATH=/path/to/type1
TYPE2_PATH=/path/to/type2
I want to run my script ./run.sh TYPE1 and do like PATTERN=$1"_PATH" now $PATTERN=TYPE1_PATH. But not sure how to get the value of $TYPE1_PATH from config
This is Bash FAQ 006.
Specifically Evaluating indirect/reference variables:
# Bash
realvariable=contents
ref=realvariable
echo "${!ref}" # prints the contents of the real variable
# ksh93 / mksh / Bash 4.3
realvariable=contents
typeset -n ref=realvariable
echo "${!ref} = $ref" # prints the name and contents of the real variable
# zsh
realvariable=contents
ref=realvariable
echo ${(P)ref} # prints the contents of the real variable

Shell Script Variable Scope with commnd

I came across an interesting thing in Shell Scripting and not 100% sure why the behaviour is like this
I tried the below script:
#!/bin/sh
CMD="curl -XGET http://../endpoint";
var1=eval $CMD | sed -e 's/find/replace/g';
echo $var1; # Output: printed the value on this line
echo $var1; # Output: blank/no data printed (Why it is blank?)
I had to change the command in variable enclosing with back-tick ` to print the variable as many time as I wanted.
CMD="curl -XGET http://../endpoint";
var1=`eval $CMD | sed -e 's/find/replace/g'`;
echo $var1; # Output: printed the value on this line
echo $var1; # Output: printed the value on this line
Why I have to surround my command with ` to assign it's o/p to the variable in subsequent variable usage?
I have a feeling that it has something to do with the variable-command scope.
Shedding light on my understanding will be appreciated!
UPDATE:
I tried the below command and it is working in my env.
#!/bin/sh
CMD="curl -XGET http://www.google.com/";
var1=eval $CMD | sed -e 's/find/replace/g';
echo $var1; # Output: printed the value on this line
echo "######";
echo $var1; # Output: blank/no data printed (Why it is blank?)
sh/bash allows you to run a command with a variable in its environment, without permanently modifying the variable in the shell. This is great, because you can e.g. run a command in a certain language just one time without having to change your entire user's or system's language:
$ LC_ALL=en_US.utf8 ls foo
ls: cannot access foo: No such file or directory
$ LC_ALL=nb_NO.utf8 ls foo
ls: cannot access foo: Ingen slik fil eller filkatalog
However, this means that when you try to do
var=this is some command
you're trigger this syntax.
It means "run the command is a command and tell it that the variable var is set to this"
It does not assign "this is my string" to the variable, and it definitely does not evaluate "this is a string" as a command, and then assign its output to var.
Given this, we can look at what actually happened:
CMD="curl -XGET http://../endpoint";
var1=eval $CMD | sed -e 's/find/replace/g'; # No assignment, output to screen
echo $var1; # Output: blank/no data printed
echo $var1; # Output: blank/no data printed
There is no scope issue and no inconsistency: the variable is never assigned, and is never written by an echo statement.
var=`some command` (or preferably, var=$(some command)) works because this is valid syntax to assign output from a program to a variable.
The first example isn't doing what you think it is.
Neither echo is printing anything. Make them echo "[$var1]" to see that.
You need the backticks to run the command and capture its output.
Your first attempt was running the $CMD | sed -e 's/find/replace/g'; pipeline with the environment of $CMD containing var1 set to a value of eval.
You also shouldn't be putting commands inside strings (or using eval in general). See http://mywiki.wooledge.org/BashFAQ/001 for more on why.

how to pass file as an argument to the script file

I have a shell script written in bash and this script should take file as an argument,can any one tell me how to write script for this any ideas on this are apprecited
Thanks,
You can access the command line arguments passed to your script using positional parameters.
Also to check if the right number of arguments have been passed to the script, you can make use of the variable $# which holds the number of arguments passed.
if [ $# -eq 1 ]; then
# exactly 1 argument was passed..use it..its available in $1
echo "Argument $1"
else
# either 0 or >1 arguments were passed...error out.
echo "Incorrect number of arguments passed"
exit 1
fi
Sample run:
$ bash a.sh
Incorrect number of arguments passed
$ bash a.sh foo
Argument foo
$ bash a.sh foo bar
Incorrect number of arguments passed
$
If you need to operate on the file, you can take the name of the file as an argument and just use the file with the specified name.
If you just need to read the contents of the file, you can use redirection to have the script read the contents of the file on standard in. You can do this using ./script < inputfile

Resources