GETOPTS passing option to create find function - linux

Im trying desperately finding the solution for my Getopts
For example
#!/bin/bash
while getopts ":a:b:" opt; do
case $opt in
a) find / $OPTARG >&2 ;;
b) 2>/dev/null >&2 ;;
\?)
echo "Invalid option: -$OPTARG" >&2
exit 1
;;
esac
and then as output example
./somefile.sh -a *txt -b
or
./somefile.sh -b -a *txt
but then i want to make sure i can upgrade it further, for examples find only sh files, or
something else.
Its not easy to find, but i hope someone could help me.

Related

Nested Case statement in shell script | Getting Syntax error

I am trying to run below shell script but i am getting syntax error.
script.sh env1 ManagedSvr1
line 29: warning: here-document at line 6 delimited by end-of-file (wanted `EOF')
line 30: syntax error: unexpected end of file
#!/bin/bash
case "$1" in "env1")
ssh weblogic#hostname1 << EOF
case "$server" in
"ManagedSvr1")
tailf /app/Oracle/Middleware/domains/dq/servers/ManagedSvr1/logs/ManagedSvr1.log
;;
"ManagedSvr2")
tailf /app/Oracle/Middleware/domains/dq/servers/ManagedSvr2/logs/ManagedSvr2.log
;;
esac
;;
"env2")
ssh weblogic#hostname2 << EOF
case "$server" in
"ManagedSvr1")
tailf /app/Oracle/Middleware/domains/dq/servers/ManagedSvr1/logs/ManagedSvr1.log
;;
"ManagedSvr2")
tailf /app/Oracle/Middleware/domains/dq/servers/ManagedSvr2/logs/ManagedSvr2.log
;;
esac
;;
esac
Your Here Docs specify EOF to end them (<< EOF) but you never have an EOF to end them. Note that EOF doesn't mean End of File, it means the string 'EOF'. https://en.wikipedia.org/wiki/Here_document has examples.
I'm not sure what you're hoping to accomplish, but it looks to me that you need to specify which file to tail. Are you hoping to pass the inner case into the remote shell on the server you're sshing into? It would simplify your code to set your filename and servername first and then ssh and execute the command. Actually, I don't see much purpose in your inner case statements anyway. Instead of wrapping everything in the 'env' case, you could just set the hostname to a variable. And then the "servername" can just be interpolated into the filesystem path. something like this seems like a simple approach:
#!/bin/bash
case "$1" in
env1)
hostname="hostname1"
;;
env2)
hostname="hostname2"
;;
esac
echo ssh weblogic#$hostname tailf /app/Oracle/Middleware/domains/dq/servers/$servername/logs/$servername.log
Seems to work simply and straight forward:
servername=ManagedSvr1 ./t.sh env1
ssh weblogic#hostname1 tailf /app/Oracle/Middleware/domains/dq/servers/ManagedSvr1/logs/ManagedSvr1.log
Take out the "echo" to actually execute the ssh.
You actually don't need "HERE" docs, per say, you're using one case statement which is taking standard input to your SSH command. take single quotes as input to ssh This lets you do multiline input in peace. Quoting your "EOF" and terminating it, is also acceptable usage.
#!/bin/bash
case "$1" in
"env1")
ssh -tt weblogic#hostname1 <<< '
case "$server" in
"ManagedSvr1")
cat /var/log/syslog
;;
"ManagedSvr2")
tailf /app/Oracle/Middleware/domains/dq/servers/ManagedSvr2/logs/ManagedSvr2.log
;;
esac '
;;
"env2")
ssh -tt weblogic#hostname2 <<< '
case "$server" in
"ManagedSvr1")
tailf /app/Oracle/Middleware/domains/dq/servers/ManagedSvr1/logs/ManagedSvr1.log
;;
"ManagedSvr2")
tailf /app/Oracle/Middleware/domains/dq/servers/ManagedSvr2/logs/ManagedSvr2.log
;;
esac '
;;
esac
This would be an easier way to manage the script. Especially as you're not utilising remote user change: Lets you set all your variables locally, and then just connect and execute the single command.
#!/bin/bash
server="$2"
case "$1" in
"env1")
hostname="hostname1"
case "$server" in
"ManagedSvr1")
remote_command=$(tailf /app/Oracle/Middleware/domains/dq/servers/ManagedSvr2/logs/ManagedSvr1.log)
;;
"ManagedSvr2")
remote_command=$(tailf /app/Oracle/Middleware/domains/dq/servers/ManagedSvr2/logs/ManagedSvr2.log)
;;
esac
;;
"env2")
hostname="hostname2"
case "$server" in
"ManagedSvr1")
remote_command=$(tailf /app/Oracle/Middleware/domains/dq/servers/ManagedSvr1/logs/ManagedSvr1.log)
;;
"ManagedSvr2")
remote_command=$(tailf /app/Oracle/Middleware/domains/dq/servers/ManagedSvr2/logs/ManagedSvr2.log)
;;
esac
;;
*)
exit 1
;;
esac
if [[ $? == 0 ]]
then ssh weblogic#$hostname $remote_command
fi

shell getopts parameters collection issue

I have below code in my shell script.
show_help()
{
cat <<EOF
Usage: ${0##*/} [-h help] [-g GATEWAY_HOSTID] [-t TIMEZONE]
-h display this help and exit
-g GATEWAY_HOSTID zabbix gateway identifier (e.g. '20225')
-t Time Zone TimeZone against which you want to test
EOF
}
OPTIND=1
while getopts "g:h:t" opt; do
case "$opt" in
h)
show_help
exit 0
;;
g)
gateway_hostid=$OPTARG
;;
t)
timezone=$OPTARG
;;
esac
done
shift $((OPTIND-1))
if [[ ! $timezone ]]; then
timezone="UTC"
fi
if [[ ! $gateway_hostid ]]; then
echo "hostid is missing!!! Exiting now."
exit
fi
When I execute script it only takes parameter gateway_hostid and ignores timezone parameter. I am not sure what I am doing wrong here. Also it doesn't show help function as well. Can someone help. below is the syntax for calling script.
./script_name.sh -g 20225 -t Europe/Zurich
./script_name.sh -g 20225 -t CEST
Your problem is with the optstring. You're specifying h: which means that -h requires an option. You are also specifying t without a : meaning t does not expect an option.
The optstring to have g and t take options and h not need one is hg:t:

How get next to next argument in linux shell script?

I have wrote simple shell script test.sh as follows:
while getopts ":A:" OPTION
do
case $OPTION in
A)
echo $OPTARG
?)
echo "no option"
esac
done
And executed the scripts as follows
$ ./test.sh -A 1 2
Now if got argument 1 by $OPTARG but how can i access the second argument ( 2 in this case)?
Regards
Jayesh
There are several options.
(1) You can use shift and take $1
while -n "$1"
do
# do something with $1
shift
done
(2) You can iterate through the args:
for i
do
# do something with $i
done
There are other alternatives also.

switches for shell script

what is the best way to implement switches [ex: -m] for shell scripts?
I can do it via the switch case statement. But i am curious to know is there any other standard way to get all the arguments into a variable via a switch.
Ex:
-m A1 A2 -c c1 c2
So that,
M[] can take -m
and C[] can all take -c
The best known way is to use getopts, see http://wiki.bash-hackers.org/howto/getopts_tutorial
An example :
#!/bin/bash
while getopts ":a" opt; do
case $opt in
a)
echo "-a was triggered!" >&2
;;
\?)
echo "Invalid option: -$OPTARG" >&2
;;
esac
done

Case statement not working in my shell script

I'm trying to write a shell script for Mac OSX Mountain Lion (10.8.2). However, no matter what I try, I can't get the switch statement to work. He is a very simplified version of the script:
#!/bin/bash
while getopts "ei" opt; do
case $opt in
e)
echo "Got option e"
;;
i)
echo "Got option i"
;;
\?)
echo "Invalid option: -$opt"
;;
esac
done
No matter what I try, I don't get any output. Any ideas? I've done scripts like this in the past.
You might be passing the options incorrectly don't forget the -
script.sh -e
Note you should also exit after an invalid option:
\?)
echo "Invalid option: -$opt"
exit 1
;;
Try replacing
\?)
echo "Invalid option: -$opt"
;;
with:
*)
echo "Invalid option: -$opt"
;;

Resources