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
Related
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
I have a shell script which asks the user as how many number of files are there in the current working directory.
I have created the script in desktop location and when I ran the script it is working fine, but when I copy and paste it in anyother folder and if I try to run, it is creating a file with name "=0".
I have posted the script too, could any one help me in resolving this issue.
#!/bin/bash
numfiles=$(ls | wc -l)
try=1
read -p "Guess the number of files present in the directory: : " num_dir
while [ $num_dir>=0 ]
do
if [ $try != 1 ]
then
read -p "Guess the number of files present in the directory: : " num_dir
fi
if [ $num_dir == $numfiles ]
then
exit
elif [ $num_dir -gt $numfiles ]
then
echo "num_dir is greater than numfiles"
else
echo "num_dir is less than numfiles"
fi
try=0
done
The error is in while [ $num_dir>=0 ] The condition redirects the "output" of $num_dir to the file =0.
You need
while [ $num_dir -ge 0 ]
Or the Bash-specific
while [[ $num_dir >= 0 ]]
test doesn't have a >= operator; it's being parsed as an output redirection, equivalent to
[ $num_dir > =0 ]
or
[ $num_dir ] > =0
(redirections are recognized by the operator, before arguments to the command are identified).
For numerical comparision, use -ge:
# Quote to avoid a syntax error if num_dir has an empty value
[ "$num_dir" -ge 0 ]
use:
while true
this way you don't get the unwanted file, also user input can be 0 which would end your loop and you can get rid of try
Why is the if-compression not comparing the value?
idle_time=exec sudo -u home xprintidle
if [ "$idle_time" -ge 6000 ]
then
echo "hi"
fi
it is not working like that
First of all, I changed to line which you use to execute xprintidle as a different user.
After that I adjusted the if clause, since there was an error as well.
#!/bin/bash
# actually assign the variable
idle_time=$(idle_time=exec sudo -u home xprintidle)
# adapted if clause to actually match (see https://stackoverflow.com/questions/18668556/comparing-numbers-in-bash)
if [ "$idle_time" -gt "6000" ]; then
echo "hi"
fi
I'm having issues with a script I'm writing in bash with regards to backing up or restoring. What I'm trying to do is check for parameters and then if none are presented, loop until a name is provided or they quit. I can check for parameters and loop to quit but the problem I am having is getting the user input and then using that for the backup file name. Here's my script so far, can someone advise on how to loop for filename/q and how to get said filename input to work with the rest of the script?
#!/bin/bash
# Purpose - Backup world directory
# Author - Angus
# Version - 1.0
FILENAME=$1.tar.gz
SRCDIR=World
DESDIR=backupfolder
if [ $# -eq 0 ]
then
echo "No filename detected. To use this utility a filename is
required. Usage:tarscript filename"
else
echo "The filename to be used will be $filename"
fi
while [ $# -eq 0 ]
do
echo "Please provide a filename or press q to exit."
read response
if [ $response == 'q' ]
then
exit
else [ $response == '$FILENAME' ]
echo -n 'Would you like to backup or restore? (B/R)'
read response
if [ $response == 'B' ]
then
tar cvpzf $DESDIR/$FILENAME $SRCDIR
echo 'Backup completed'
exit
fi
fi
done
I finally managed to get it working in the end. I realised what my mistakes were thanks to Jens and changed things enough that it now responds to input and supplied parameters. Of course the code is nearly twice as big now with all my changes but hey ho.
I have a shell script where I pass (2) parameters, one to pass a dbname, the other to call one of (2) filenames. I want to check if either filename exists, then proceed with calling that script, else exit because the user can enter the wrong string and construct my_foo.sql which I don't want. I don't think I have the condition for setting "or" correctly since putting the correct param still produces error. Is there a better way to write this?
Here is what I have so far.
#/usr/bin/ksh
if [ $# != 2 ]; then
echo "Usage: test.sh <dbname> <test|live>" 2>&1
exit 1
fi
# Check actual file name
CHKSCRIPT1=/tmp/my_test.sql;
CHKSCRIPT2=/tmp/my_live.sql;
if [ -f "CHKSCRIPT1" ] || [ -f "CHKSCRIPT2" ]
then
/bin/sqlplus -s foo/bar #/my_$2.sql
else
echo "Correct sql script does not exist. Enter test or live"
exit 1
fi
Your issue is that you're not referencing your variables correctly:
if [ -f "$CHKSCRIPT1" ] || [ -f "$CHKSCRIPT2" ]
...
fi
edit: Per #chepner, you shouldn't use -o
In addition to the problem you had with expanding the parameters, you should separate what the user types from what files need to exist. If the user enters "live", the only thing that matters is whether or not /tmp/my_live.sql exists. If the user enters "injection_attack", your script should not execute /tmp/my_injection_attack.sql (which presumably was created without your knowledge). The right thing to do is to first verify that a valid command was entered, then check if the appropriate file exists.
if [ $# != 2 ]; then
echo "Usage: test.sh <dbname> <test|live>" 2>&1
exit 1
fi
case $2 in
test|live)
filename="/tmp/my_{$2}.sql"
;;
*) echo "Must enter test or live"
exit 1
;;
esac
if [ -f "$filename" ]; then
/bin/sqlplus -s foo/bar #/my_$2.sql
else
echo "SQL script $filename does not exist."
exit 1
fi