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.
Related
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} "$*"
I'm trying to create a bash function that will use less to apply a pattern and follow the file using the argument passed to the function
my_less_function() {
if [ -z "$1" ]
then
# if no arg
less +F /var/log/my.log
else
# else, filter out the arg
less +$'&!'$1'\nF' /var/log/my.log
fi
}
my issue is that i can't get the arg to substitute properly in the else block
my_less_function MY_VALUE displays Non-match &/MY_VALUE\nF in less
it looks like it's concatenating the argument and \nF, but \nF is supposed to trigger the follow command instead of being interpreted as part of the argument
any ideas?
wrong : less +$'&!'$1'\nF' /var/log/my.log
right : less +$'&!'${1}$'\nF' /var/log/my.log
I got two variables in a bash script. One contains the name of a function within the script while the other one is an array containing KEY=VALUE or KEY='VALUE WITH SPACES' pairs. They are the result of parsing a specific file, and I can't change this.
What I want to do is to invoke the function whose name I got. This is quite simple:
# get the value for the function
myfunc="some_function"
# invoke the function whose name is stored in $myfunc
$myfunc
Consider the function foo be defined as
function foo
{
echo "MYVAR: $MYVAR"
echo "MYVAR2: $MYVAR2"
}
If I get the variables
funcname="foo"
declare -a funcenv=(MYVAR=test "MYVAR2='test2 test3'")
How would I use them to call foo with the pairs of funcenv being added to the environment? A (non-variable) invocation would look like
MYVAR=test MYVAR2='tes2 test3' foo
I tried to script it like
"${funcenv[#]}" "$funcname"
But this leads to an error (MYVAR=test: command not found).
How do I properly call the function with the arguments of the array put in its environment (I do not want to export them, they should just be available for the invoked function)?
You can do like this:
declare -a funcenv=(MYVAR=test "MYVAR2='test2 test3'")
for pairs in "${funcenv[#]}"; do
eval "$pairs"
done
"$funcname"
Note however that the variables will be visible outside the function too.
If you want to avoid that, then you can wrap all the above in a (...) subshell.
why don't you pass them as arguments to your function?
function f() { echo "first: $1"; echo "second: $2"; }
fn=f; $fn oneword "two words"
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
I have two shell scripts say 1.ksh & 2.ksh
1.ksh contains functions which has variables in it.
e.g.
func1() {
test=$DIRPATH/$FILENAME/$SCRIPTNAME
}
2.ksh contains
DIRPATH="/ABC/DEF/GHI" .... {PATH}
FILENAME="ABC.txt"
I want to invoke 1.ksh inside 2.ksh & pass these parameters to respective functions.
Also, I want to run both the script in a single instance.
How can I achieve this?
You might be looking for . dot command
The . fileNmae can be used to export all varibles function onto the bash script involving the statements
To use the function func1 from 2.ksh use
``2.ksh` script edited as
#2.ksh contains
. ./1.ksh #full path must be something like /home/....
DIRPATH="/ABC/DEF/GHI" .... {PATH}
FILENAME="ABC.txt"
func1 #calls the function within the second, 2.ksh
For example consider the following
$ cat one
func1() {
echo "hello $1"
}
$ cat two
. ./one
func1 world
$ ksh two
hello world