get empty value when passing two parametres to shell script - bash

I am trying to extract two variables which I passed to shell script like :
/XDRI_RV_LOAD.KSH 20220617 rv_tiers
Inside the shell script, I write :
date_ctrlm=$1
branche=$2
echo "TEST 1 : " $date_ctrlm
echo " TEST 2 :" $branche
So that I get this
TEST 1 : 20220617
TEST 2 :
I don't know why the job does not extract the 2 param. In fact, I should get also
TEST 2 : rv_tiers
Thanks

The solution is to replace :
jobInit ${0} "$#"
By
jobInit ${0} "$*"

Related

Strange Behavior in Bash For Loop

Given the following code, BASH's output is unexpected for me and I'm looking for possible solutions (I've tried changing the way I'm quoting but that doesn't seem to affect anything to produce the desired result):
Testing File:
#!/bin/bash
. FindMissingSettings.function
Settings[0]="FirstSetting"
Settings[1]="SecondSetting"
Settings[2]="ThirdSetting"
ThisFile="ThisFile"
find_missing_settings "${Settings[#]}" "$ThisFile"
The included FindMissingSettings.function:
#!/bin/bash
find_missing_settings () {
Settings=("$#")
File=$2
for Setting in "${Settings[#]}"; do
echo "$Setting"
echo "1"
done
echo "$File"
echo "2"
}
I expected the output from this script and the included function to be:
FirstSetting
1
SecondSetting
1
ThirdSetting
1
ThisFile
2
However this was the result I received:
FirstSetting
1
SecondSetting
1
ThirdSetting
1
ThisFile
1
SecondSetting
2
Why is this and what can I do to provide the desired result? Thank you!
In your find_missing_settings function, in your variable Setting, you have all the given inputs (FirstSetting, Second Setting, ThirdSetting, ThisFile). That's why it print it all with during the loop.
Then it print the 2nd setting in the list, so SecondSetting
To fix this, you can put ThisFile as first parameter of the function:
find_missing_settings "$ThisFile" "${Settings[#]}"
And change in the find_missing_settings function how you get the inputs:
Settings=("${#:2}")
File=$1
The :2 ask to get the inputs starting from the 2nd one only, and you put the first one (ThisFile) in the variable File

How to give an empty array to function in bash? [duplicate]

This question already has answers here:
How to pass array as an argument to a function in Bash
(8 answers)
Closed 6 years ago.
I am learning bash.
Now I would like to give an empty array to a function. However, it does not work. Please refer to following,
function display_empty_array_func() {
local -a aug1=$1
local -a aug2=${2:-no input}
echo "array aug1 = ${aug1[#]}"
echo "array aug2 = ${aug2[#]}"
}
declare -a empty_array=()
display_empty_array_func "${empty_array[#]}" "1"
The outputs are following,
array aug1 = 1 # I expected it is empty
array aug2 = no input # I expected it is 1
In my understanding, quoting variable allow us to give an empty variable,
like following,
function display_empty_variable_func() {
local aug1=$1
local aug2=${2:-no input}
echo "variable aug1 = ${aug1}"
echo "variable aug2 = ${aug2}"
}
display_empty_variable_func "" "1"
And its outputs are following
variable aug1 = # it is empty as expected
variable aug2 = 1 # it is 1 as expected
I don't know what is the problem with passing am empty array.
Someone who knows mechanism or solutions. Please let me know it.
Thank you very much.
enter image description hereIf positional parameter is empty, shell script & shell function will not consider that value and instead it took the next non empty value as its value(positional parameter value).
If we want to take the empty value, must we have to put that value in quotes.
Ex: I'm having small script like
cat test_1.sh
#!/bin/bash
echo "First Parameter is :"$1;
echo "Second Parameter is :"$2;
case -1
If i executed this script as
sh test_1.sh
First Parameter is :
Second Parameter is :
Above two lines are empty because i have not given positional parameter values to script.
case-2
If i executed this script as
sh test_1.sh 1 2
First Parameter is :1
Second Parameter is :2
case-2
If i executed this script as
sh test_1.sh 2 **# here i given two spaces in between .sh and 2 and i thought 1st param is space and second param is 2 but**
First Parameter is :2
Second Parameter is :
The output looks like above. My first statement will apply here.
case-4
If i executed this script as
sh test_1.sh " " 2
First Parameter is :
Second Parameter is :2
Here i kept space in quotes. Now i'm able to access the space value.
**This will be help full for you. But for your requirement please use below code.**
In this you have **quote** the space value(""${empty_array[#]}"") which is coming from **an empty array**("${empty_array[#]}"). So you have to use additional quote to an empty array.
function display_empty_array_func() {
local -a aug1=$1
local -a aug2=${2:-no input}
echo "array aug1 = ${aug1[#]}"
echo "array aug2 = ${aug2[#]}"
}
declare -a empty_array=()
display_empty_array_func ""${empty_array[#]}"" "1"
The output is:
array aug1 =
array aug2 = 1

Pass array and string from one script to another script

Hi have two script one in calling the another.
from one script I m trying to pass two parameters a string and an array.
How can i receive it on another script.
Script 1 :
$admin_script_path/aggregation_checklist_hdfs.sh "${aggregation_hdfs_folder}" "${incrementalArray[#]}"
Script 2 :
path="$1"
echo "$path"
specificAggregationArray=( "$#" )
but these are not working.

How to get names of variables are available/set in shell scripts

I want all variables names are set in shell script. I have a file which contains key value pairs and I was read content from that file and store/set into variables. I want to do some processes if a variable is available/set otherwise I don't need to do those processes. How to achieve this.
For example I run loop in shell scripts in each iteration it gives one of the variables is set before that command.
If code like this
a=test1
b=test2
c=test3
for i in ???
do
echo $i
done
then I want output like this
a
b
c
What command is used o achieve this.
You could use set before and after setting the variables
e.g:
$ set > aux1
$ c=345
$ set > aux2
$ diff aux1 aux2
57c57
< PIPESTATUS=([0]="141" [1]="0")
---
> PIPESTATUS=([0]="0")
112a113
> c=345
If you have a pre-defined list of such variables, then you can test it like this:
for i in $(echo "a b c"); do
echo $i
done
If i could help you :
#!/bin/sh
# Define list of tests
LIST_TESTS=`cat list_test.txt`
for TEST in ${LIST_TESTS}
do
vartest=`echo ${TEST}`
if [ "${vartest}" = "" ]
# No Test
then
echo "*** WARNING*** Test not found"
else
echo "${vartest} is available"
fi
done
# Second Define list of tests
tabTest=('test1' 'test2' 'test3')
i=0
while [ "${tabTest[$i]}" != "" ]
do
echo "${tabTest[$i]} is available"
i=$(($i+1))
done

Variable affectation from function not working with $()

I'm trying to define a function which behaviour that will, the first time it has been called, ask the user for some choice and then remember thoses choice in order not to ask the user again.
Using a variable to store the state (initialized or not) works well when the function is called the normal way, however if I want this function to return results, capturing thoses results with $() breaks the behaviour I'm trying to achieve
Here is a simple reproductible exemple:
#!/bin/bash
initialized=false
function givevalue
{
echo "init = $initialized" 1>&2
if ! $initialized;
then
echo "a b c"
initialized=true
else
echo "d e f"
fi
}
for i in $(givevalue); do echo "run 1 : $i"; done
for i in $(givevalue); do echo "run 2 : $i"; done
the result I get is
init = false
run 1 : a
run 1 : b
run 1 : c
init = false
run 2 : a
run 2 : b
run 2 : c
while I was expecting
init = false
run 1 : a
run 1 : b
run 1 : c
init = true
run 2 : d
run 2 : e
run 2 : f
The contents of $(...) are run as a separate process and changes to variables are not propagated to the parent shell. This cannot be changed.

Resources