Propagate values in shell script - linux

I have a simple script that uses CAPDEPTH like a count variable and calls some tests for each value.
#!/bin/bash
# SCRIPT ONE
CAPDEPTH=1
...
while [ $CAPDEPTH -lt 11 ];
do
echo "cap depth - $CAPDEPTH"
make test-all-basics
let CAPDEPTH=CAPDEPTH+1
done
And in line
eval "make test-all-basics"
It will do multiple calls to another shell script which I also want to make dependent on value of CAPDEPTH. Here are couple of lines from that script.
# SCRIPT TWO
...
R_binary="${R_HOME}/bin/exec${R_ARCH}/R"
capture_arg="--tracedir $(CAPDEPTH)"
...
My question is how to get the value of CAPDETH propagated from SCRIPT ONE to SCRIPT TWO. Is that even possible?
I've tried export of the variable CAPDEPTH in both scripts, but does not seems to work.

#!/bin/bash
# SCRIPT ONE
CAPDEPTH=1
...
while [ $CAPDEPTH -lt 11 ]; do
echo "cap depth - $CAPDEPTH"
export CAPDEPTH # Makes it so that any child process inherits the variable CAPDEPTH and anything it contains.
make test-all-basics
let CAPDEPTH++ # Increments CAPDEPTH by +1
done
# SCRIPT TWO
...
R_binary="${R_HOME}/bin/exec${R_ARCH}/R"
capture_arg="--tracedir $CAPDEPTH"
...

Inside script one use,
export CADEPTH=....
let CADEPTH=...
echo $CADEPTH
and inside script two, do:
source script1
and then,
echo $CADEPTH

Related

How to put array in command-string so that I can eval it?

I've been struggling with this problem for a while. Let's assume I have two scripts.
test1.sh
test2.sh
The code in test1.sh is the following:
array1="/dir/file1.txt /dir/file2.txt /dir/file3.txt"
array2="/dir/file4.txt /dir/file5.txt /dir/file6.txt"
./test2.sh "$array1" "$array2"
The code in test2.sh is the following:
echo $1
echo $2
This works fine, and prints the two arrays correctly:
/dir/file1.txt /dir/file2.txt /dir/file3.txt
/dir/file4.txt /dir/file5.txt /dir/file6.txt
For the project I am working on I have to put the execution code in a variable so that I can run it with the eval-command. I've tried it as follows:
array1="/dir/file1.txt /dir/file2.txt /dir/file3.txt"
array2="/dir/file4.txt /dir/file5.txt /dir/file6.txt"
com="./test2.sh "$array1" "$array2" "
eval $com
However, this returns:
/dir/file1.txt
/dir/file2.txt
How do I get it to give the same input? I've been struggling with this for a while now and Im honestly pretty stuck. I believe it is caused by the many quatation marks in com-variable, but I am not sure.
Many thanks,
Patrick
Make com an array, and you don't need eval.
#!/usr/bin/env bash
# ^^^^- NOT /bin/sh. Run with "bash yourscript", not "sh yourscript"
# none of these are actually arrays; they're just misleadingly-named strings
array1="/dir/file1.txt /dir/file2.txt /dir/file3.txt"
array2="/dir/file4.txt /dir/file5.txt /dir/file6.txt"
# This is an actual array.
com=( ./test2.sh "$array1" "$array2" )
# Expand each element of the array into a separate word of a simple command
"${com[#]}"

Increment Number (variable) in bash script

I need to increment a number inside a varaible in a bash script.
But after the script is done, the variable should be exported with the new number and available next time the script is running.
IN MY SHELL
set x=0
SCRIPT
" If something is true.. do"
export x=$(($x+1)) //increment variable and save it for next time
if [ $x -eq 3 ];then
echo test
fi
exit
You cannot persist a variable in memory between two processes; the value needs to be stored somewhere and read on the next startup. The simplest way to do this is with a file. (The fish shell, which supports "universal" variables, uses a separate process that always runs to communicate with new shells as they start and exit. But even this "master" process needs to use a file to save the values when it exits.)
# Ensure that the value of x is written to the file
# no matter *how* the script exits (short of kill -9, anyway)
x_file=/some/special/file/somewhere
trap 'printf '%s\n' "$x" > "$x_file"' EXIT
x=$(cat "$x_file") # bash can read the whole file with x=$(< "$x_file")
# For a simple number, you only need to run one line
# read x < "$x_file"
x=$((x+1))
if [ "$x" -eq 3 ]; then
echo test
fi
exit
Exporting a variable is one way only. The exported variable will have the correct value for all child processes of your shell, but when the child exits, the changed value is lost for the parent process. Actually, the parent process will only see the initial value of the variable.
Which is a good thing. Because all child processes can potentially change the value of an exported variable, potentially messing things up for the other child processes (if changing the value would be bi-directional).
You could do one of two things:
Have the script save the value to a file before exiting, and
reading it from the file when starting
Use source your-script.bash or . your-script.bash. This way, your
shell will not create a child process, and the variable gets changed in
same process

Checking cmd line argument in bash script bypass the source statement

I have an bash script "build.sh" like this:
# load Xilinx environment settings
source $XILINX/../settings32.sh
cp -r "../../../EDK/platform" "hw_platform"
if [ $# -ne 0 ]; then
cp $1/system.xml hw_platform/system.xml
fi
echo "Done"
Normally I run it as "./build.sh" and it execute the "source" statement to set environment variables correct. Sometimes I need to let the script to copy file from an alternative place, I run it as "./build.sh ~/alternative_path/"; My script check whether there is an cmd line argument by checking $# against 0.
When I do that, the "source" statement at the beginning of the script somehow get skipped, and build failed. I have put two "echo" before and after the "source", and I see echo statements get executed.
Currently I circumvent this issue by "source $XILINX/../settings32.sh; build.sh". However, please advise what I have done wrong in the script? Thanks.
Try storing the values of your positional paramaters first on an array variable then reset them to 0. "$XILINX/../settings32.sh" may be acting differently when it detects some arguments.
# Store arguments.
ARGS=("$#")
# Reset to 0 arguments.
set --
# load Xilinx environment settings
source "$XILINX/../settings32.sh"
cp -r "../../../EDK/platform" "hw_platform"
if [[ ${#ARGS[#]} -ne 0 ]]; then
cp "${ARGS[0]}/system.xml" hw_platform/system.xml
fi
echo "Done"

Bash config file or command line parameters

If I am writing a bash script, and I choose to use a config file for parameters. Can I still pass in parameters for it via the command line? I guess I'm asking can I do both on the same command?
The watered down code:
#!/bin/bash
source builder.conf
function xmitBuildFile {
for IP in "{SERVER_LIST[#]}"
do
echo $1#$IP
done
}
xmitBuildFile
builder.conf:
SERVER_LIST=( 192.168.2.119 10.20.205.67 )
$bash> ./builder.sh myname
My expected output should be myname#192.168.2.119 and myname#10.20.205.67, but when I do an $ echo $#, I am getting 0, even when I passed in 'myname' on the command line.
Assuming the "config file" is just a piece of shell sourced into the main script (usually containing definitions of some variables), like this:
. /etc/script.conf
of course you can use the positional parameters anywhere (before or after ". /etc/..."):
echo "$#"
test -n "$1" && ...
you can even define them in the script or in the very same config file:
test $# = 0 && set -- a b c
Yes, you can. Furthemore, it depends on your architecture of script. You can overwrite parametrs with values from config and vice versa.
By the way shflags may be pretty useful in writing such script.

Accessing variable from ARGV

I'm writing a cPanel postwwwact script, if you're not familiar with the script its run after a new account is created. it relies on the user account variable being passed to the script which i then use for various things (creating databases etc). However, I can't seem to find the right way to access the variable i want. I'm not that good with shell scripts so i'd appreciate some advice. I had read somewhere that the value i wanted would be included in $ARGV{'user'} but this simply gives "root" as opposed to the value i need. I've tried looping through all the arguments (list of arguments here) like this:
#!/bin/sh
for var
do
touch /root/testvars/$var
done
and the value i want is in there, i'm just not sure how to accurately target it. There's info here on doing this with PHP or Perl but i have to do this as a shell script.
EDIT Ideally i would like to be able to call the variable by something other than $1 or $2 etc as this would create issues if an argument is added or removed
..for example in the PHP code here:
function argv2array ($argv) {
$opts = array();
$argv0 = array_shift($argv);
while(count($argv)) {
$key = array_shift($argv);
$value = array_shift($argv);
$opts[$key] = $value;
}
return $opts;
}
// allows you to do the following:
$opts = argv2array($argv);
echo $opts[‘user’];
Any ideas?
The parameters are passed to your script as a hash:
/scripts/$hookname user $user password $password
You can use associative arrays in Bash 4, or in earlier versions of Bash you can use built up variable names.
#!/bin/bash
# Bash >= 4
declare -A argv
for ((i=1;i<=${##};i+=2))
do
argv[${#:i:1}]="${#:$((i+1)):1}"
done
echo ${argv['user']}
Or
#!/bin/bash
# Bash < 4
for ((i=1;i<=${##};i+=2))
do
declare ARGV${#:i:1}="${#:$((i+1)):1}"
done
echo ${!ARGV*} # outputs all variable names that begin with ARGV
echo $ARGVuser
Running either:
$ ./argvtest user dennis password secret
dennis
Note: you can also use shift to step through the arguments, but it's destructive and the methods above leave $# ($1, $2, etc.) in place.
#!/bin/bash
# Bash < 4
# using shift (can use in Bash 4, also)
for ((i=1;i<=${##}+2;i++))
do
declare ARGV$1="$2"
# Bash 4: argv[$1}]="$2"
shift 2
done
echo ${!ARGV*}
echo $ARGVuser
If it's passed as a command-line parameter to the script, it's available as $1 if it's first parameter, $2 for the second, and so on.
Why not start off your script with something like
ARG_USER=$1
ARG_FOO=$2
ARG_BAR=$3
And then later in your script refer to $ARG_USER, $ARG_FOO and $ARG_BAR instead of $1, $2, and $3. That way, if you decide to change the order of arguments, or insert a new argument somewhere other than at the end, there is only one place in your code that you need to update the association between argument order and argument meaning.
You could even do more complex processing of $* to set your $ARG_WHATEVER variables, if it's not always going to be that all of the are specified in the same order every time.
You can do the following:
#!/bin/bash
for var in $argv; do
<do whatver you want with $var>
done
And then, invoke the script as:
$ /path/to/script param1 arg2 item3 item4 etc

Resources