Multiparameter in shell script - shell

So, I have a simple script whose only purpose is to help me in my lazyness by allowing me to type less thing when setting acl for someone using blih
#!/bin/sh
if [ "$1" "$2" ]; then
~/.blih.py -u X.X#X.eu repository setacl "$1" "$2" rw
fi
I've named it setacl.sh and set it as an aliases in my .bash_aliases
alias setacl='~/.bash_scripts/setacl.sh'
and yet when I use it, I get the following
setacl Java_epicture_2017 X
/root/.bash_scripts/setacl.sh: 3: [: Java_epicture_2017: unexpected operator
What am I doing wrong?

If you're trying to verify that both arguments are set, write:
if [ -n "$1" ] && [ -n "$2" ]
Or more simply, check the number of arguments passed:
if [ "$#" -eq 2 ]

Related

Equal and not equal operators not working in bash script

I have this function inside a .sh script :
prepare_for_test(){
stopresources;
initresources;
if [ "$1" = "--gf" ]; then
startglassfish;
fi
docker ps;
notify-send "Done!" "You can now test" -t 10000;
};
The script's name's preparefortests.sh. When I run it on bash, passing --gf or "--gf":
preparefortests.sh --gf
it does not run alias startglassfish, as if that if statement was false.
I even tried to add a check on the parameter:
if [ "$1" ] && [ "$1" != "--gf" ]; then
echo "uknown parameter $1"
fi
but it's not working neither, when e.g. I try to run it like:
preparefortests.sh hello
I'd expect "unknown parameter hello".
What am I doing wrong?
The comparison statement is correct:
if [ "$1" = "--gf" ]; then
startglassfish;
fi
There can be other issue like:
Make sure you pass $1 argument, while calling function:
Write prepare_for_test $1
The problem might be the alias used. For almost every purpose, aliases are superseded by shell functions. So either you need to make alias as function and export it or instead use special variable BASH_ALIASES. In your case:
if [ "$1" = "--gf" ];then
${BASH_ALIASES[startglassfish]};
fi

Install multiple packages via bash script with dnf

I'm currently writing a bash script which should install all the software that I need. The process looks promising so far: First, I write a "software-list.txt" file which contains all dependencies for multiple distros. Afterwards bash split these values into an array and reads the corresponding value out of it. Finally the script should combine the distro package manager name (e.g. dnf, if I'm using Fedora Linux) with the operator ("install") with the arguments (which are the software packages).One last info: All variable names which don't appear in the source code, we're defined beforehand
The script looks like this:
One last info: All variable names which don't appear in the source code, were defined beforehand
case "$DISTRO_NAME" in
"Fedora")
PROGRAMM="dnf"
CSV_INDEX=0;;
"Debian")
PROGRAMM="apt-get"
CSV_INDEX=1;;
esac
# Read all required packages
while IFS= read -r line
do
IFS=','
LINE=($line)
if [ $CURR_LINE_INDEX -gt 1 ] && [ $CURR_LINE_INDEX -lt $LINE_COUNT ]
then
ARGUMENTS+="${LINE[$CSV_INDEX]} "
elif [ $CURR_LINE_INDEX -eq $LINE_COUNT ]
then
ARGUMENTS+="${LINE[$CSV_INDEX]}"
fi
CURR_LINE_INDEX=$((CURR_LINE_INDEX+1))
done < "software-list.txt"
# Run installation script
$PROGRAMM $OPERATOR $ARGUMENTS
However, whenever I run the script, the command is correct. But the output is always the same "couldn't find any match for packagex packagey"
I followed Jetchisel's advice and did a shellcheck. How is it now?
#!/bin/bash
case "$DISTRO_NAME" in
"Fedora")
PROGRAMM="dnf"
CSV_INDEX=0;;
"Debian")
PROGRAMM="apt-get"
CSV_INDEX=1;;
esac
# Read all required packages
while IFS= read -r -a line
do
IFS=','
LINE=("${line[#]}")
if [ "$CURR_LINE_INDEX" -gt 1 ] && [ "$CURR_LINE_INDEX" -lt "$LINE_COUNT" ]
then
ARGUMENTS+="${LINE[$CSV_INDEX]} "
elif [ "$CURR_LINE_INDEX" -eq "$LINE_COUNT" ]
then
ARGUMENTS+="${LINE[$CSV_INDEX]}"
fi
CURR_LINE_INDEX=$((CURR_LINE_INDEX+1))
done < "software-list.txt"
# Run installation script
"$PROGRAMM $OPERATOR $ARGUMENTS"

Shell Script compare the values with input parameter

apps="http:git.abc.com";
cluster-ui="http:git.xyz.com";
customer-ui="http:git.xxx.com";
SERVICE=$1;
My requirement is if I pass service name as a 'apps' then I need to clone the $apps url.
Here
if [ $Service -eq apps ]
not think a good approach as my repo url might get increased so more and more loop will come
Any suggestions?
The $ sign assigns the input argument, so we're getting first input if it matches the below variable, so do what you want inside if condition.
#!/bin/bash
apps="http:git.abc.com";
clusterui="http:git.xyz.com";
customerui="http:git.xxx.com";
#SERVICE=$1;
#Store global
repo=''
# if empty parameter is passed
if [ $# -lt 1 ] ; then
echo "Parameters Need"
exit 1
fi;
# for search the correct parameter
if [ $1 = "apps" ]; then
repo=$apps
elif [ $1 = "cluster-ui" ] ; then
repo=$clusterui
elif [ $1 = "customer-ui" ] ; then
repo=$customerui
else
echo "Not found"
fi;
echo $repo
Note just repeat elif [ ] ;then for more entries or think!
how to access run this file like this sh ./file.sh apps just replace apps with yours. make sure you have permission to execute the file if you don't have, give it to permission like below
chmod 766 file
now run the shell script sh ./file.sh clusterui
'Case statement' would suit here more than if ladder

Checking if any of several globs is matched in shell

I've troubles to understand an if syntax of a line in shell:
if [ ! -f *file1.txt* -a ! -f *file2.txt* -a ! -f *file3.txt* ]; then
sbatch file.sh
fi
The * is used because my files are backed up to #file.txt.1# format.
As far as I know, the ! creates a 'if not', the -f 'if the string is a file' but I haven't found any function for the -a flag.
I want to submit the file.sh only if all these files are NOT present.
Does anyone could help?
One easy implementation, compatible with any POSIX shell:
exists_any() {
while [ "$#" -gt 0 ]; do # as long as we have command-line arguments...
[ -e "$1" ] && return 0 # if first argument names a file that exists, success
shift # remove first argument from the list
done
return 1 # nothing matched; report failure
}
if ! exists_any *file1.txt* *file2.txt* *file3.txt*; then
sbatch file.txt
fi

"[: too many arguments" appears occasionally with pidof

cplane_pid=`pidof hnb_gw.exe`
if [ -z $cplane_pid ]
then
STATUS=`failure`
echo "Cplane hnbgw running $STATUS"
else
STATUS=`success`
echo "Cplane hnbgw running $STATUS"
fi
echo
If there are multiple instances of hnb_gw.exe, pidof will return multiple pids. The -z of [ expects only one pid. One solution might be to use the -s switch of pidof to return only one pid.
You need to Use More Quotes™:
if [ -z "$cplane_pid" ]
Adding set -x before and set +x after the command shows you what it results in. For example:
$ cplane_pid="1 2 3"
$ set -x
$ [ -z $cplane_pid ]
+ '[' -z 1 2 3 ']'
bash: [: too many arguments
In other words, each of the whitespace-separated values in the variable was used as a single parameter. Since -z requires exactly one parameter, this results in a syntax error.
Rather than saving this as a variable, you can simply do
if ! pidof hnb_gw.exe > /dev/null
If the process doesn't exist, it will return 1 ("false").
When you execute
cplane_pid=`pidof hnb_gw.exe`
then cplane_pid can contain more (space separated) items.
So the expansion in
if [ -z $cplane_pid ]
will become
if [ -z firstPid secondPid etc ]
and that is your error "[: too many arguments"
You can solve this with quoting the variable (you should do this ALWAYS in shell)
if [ -z "$cplane_pid" ]
or use [[ (if it's installed on your system), which is better in many ways. For instance you don't need to quote variable :)
if [[ -z $cplane_pid ]]
is the same as
if [[ -z "$cplane_pid" ]]
For testing purposes (and erros like this) use -x hasbang bash option
#!/bin/bash -x
or use debug sections
-- normal code --
set -x # debug section starts here
[ -z $cplane_pid ] && echo zero
eval something
set +x # debug section ends here
-- normal code --
also you can call the script
/bin/bash -x yourScript.sh
pidof can return more than one pid, in these cases, your test will get too many arguments.

Resources