How to find a parameter value using input in shell script - shell

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

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

Double Parameter in shell scripting throwing error

I am trying to make my scripts more generic and hence trying to pass parameter.
I have config file which contains variables (which are used in the scripts) and in the scripts ,I am sourcing (source command) the file in another scripts (ksh).
Config file contains:
p2020_m23_ORACLE_USERNAME=sanjeeb
Parameter for the script is p2020_m23.
ksh script:
export SOURCE_CD=$1
export CONFIG_FILE=/user/spanda20/dbconfig.txt
source $CONFIG_FILE
USERNAME=${${SOURCE_CD}_ORACLE_USERNAME} << **This throws error** >>
USERNAME=$p2020_m23_ORACLE_USERNAME <<< **This gives correct result** >>
manual test:
[spanda2 config]$ export SOURCE_CD=p2020_m23
[spanda2 config]$ export m23_ORACLE_USERNAME=sanjeeb
[spanda2 config]$ export USERNAME=${${SOURCE_CD}_ORACLE_USERNAME}
-bash: USERNAME=${${SOURCE_CD}_ORACLE_USERNAME}: bad substitution
USERNAME_REF="${SOURCE_CD}_ORACLE_USERNAME"
USERNAME="${!USERNAME_REF}"
${parameter} -The value of parameter is substituted
so If you want to append the value of 2 variables and assign in other . You should have written in like this
export SOURCE_CD=p2020_m23
export m23_ORACLE_USERNAME=sanjeeb
export USERNAME="${SOURCE_CD}_${ORACLE_USERNAME}"
In ksh you can use variable indirection with typeset -n or nameref.
Simple example:
$ typeset -n that
$ this=word
$ that=this
$ echo $that
word
$ this=nothing
$ echo $that
nothing
The name reference now makes $that return the current value of $this.

what is the significance of exporting path in shell script?

I have seen below two lines in a shell script.
Im new to unix scripting, what is the use of setting this?
PATH=$PATH:/bin:/usr/bin:/usr/sbin:/sbin:/etc:/usr/ucb:/usr/ccs/bin:/usr/local/bin
export PATH
Thanks in advance
If you export something (in bash anyway which I assume is your shell), it will mark that something to be available in subsequently executed commands.
$ FOO=1 # Set the variable
$ echo $FOO # Check the value
1
$ bash # New shell here.
$ echo $FOO # No value since it's not exported
$ exit # Quit the subshell
$ export FOO # Export it
$ bash
$ echo $FOO # It has a value now
1
export is a shell builtin for bash so doing a help export will give you more information on it.
Explicitly exporting the PATH doesn't hurt but generally has no effect as the PATH variable is almost certainly already marked as exported when you launch a shell script.

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

How can you get a variable's value given its name in korn shell?

Is there a way in ksh to get a variable's value when you have been given the name of the variable?
For example:
#!/usr/bin/ksh
var_name=$1 #pretend here that the user passed the string "PATH"
echo ${$var_name} #echo value of $PATH -- what do I do here?
eval `echo '$'$var_name`
echo concatenates a '$' to the variable name inside $var_name, eval evaluates it to show the value.
EDIT:
The above isn't quite right. The correct answer is with no backticks.
eval echo '$'$var_name
printenv is not a ksh builtin and may not always be present. For older ksh versions, prior to ksh93, the eval 'expression' method works best.
A powerful method in ksh93 is to use indirection variables
with 'nameref' or 'typeset -n'.
Define and verify a nameref variable that refers to $PATH:
$ nameref indirect=PATH
$ print $indirect
/usr/bin:/usr/sbin
See how the nameref variable changes when we change PATH:
$ PATH=/usr/bin:/usr/sbin:/usr/local/bin
$ print $indirect
/usr/bin:/usr/sbin:/usr/local/bin
Show ksh version and the alias for nameref:
$ type nameref
nameref is an alias for 'typeset -n'
$ echo ${.sh.version}
Version JM 93t+ 2010-02-02
var_name=$1 #pretend here that the user passed the string "PATH"
printenv $var_name
For one step above your answer (I spent a lot of time trying to find both these answers). The below will allow you to export a dynamic variable and then recall it dynamically:
echo -n "Please provide short name for path:"
read PATH_SHORTCUT
echo -n "Please provide path:"
read PATH
eval export \${PATH_SHORTCUT}_PATH="${PATH}"
eval echo Path shortcut: ${PATH_SHORTCUT} set to \$"${PATH_SHORTCUT}_PATH".

Resources