Linux - ServerThis forum is for the discussion of Linux Software used in a server related context.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Michael is correct, and I'll note you can install shellcheck locally if you don't want to run it online, and there are even editor plugins available to make it even easier.
Also, simply saying "it fails" is useless - in future make sure to always give the actual error messages and/or describe in what way it is failing.
Michael is correct, and I'll note you can install shellcheck locally if you don't want to run it online, and there are even editor plugins available to make it even easier.
Also, simply saying "it fails" is useless - in future make sure to always give the actual error messages and/or describe in what way it is failing.
Sorry about that... Here is what I get.
Code:
24-Sep-2021 15:34:52.469 dns_rdata_fromtext: buffer-0x7ff448cdce60:1: near 'b': bad dotted quad
invalid rdata format: bad dotted quad
syntax error
rm1=$1
rm2=$2
rec1=$3
input_validation ()
{
if [ rm1=$1 ] && [ rm2=$2 ]; then
if [ $# -ne 2 ]; then
usage
fi
elif [ rm1=$1 ] && [ rm2=$2 ] && [ rec1=$3 ]; then
if [ $# -ne 3 ]; then
usage2
fi
elif [ rm1=$1 ] && [ rec1=$2 ]; then
if [ $# -ne 2 ]; then
usage1
fi
fi
Code:
if [ "$rec1" == "a" ]; then
...command
...command
elif [ "$rec1" == "b" ]; then
...command
...command
else
...command
...command
fi
It fails on the bold if statement ... I dont know a way around.
First IF statement needs inputs 1 and 2.
Second IF statement needs inputs 1 and 2 and 3.
Third IF statement needs inputs 1 and 3 but not 2.
Please help. Thank you!
Pretty much everything about the first code snippet is wrong.
Please tell us what you're trying to achieve, we might be able to assist.
I also added the following code under my inputs...
Code:
update ()
{
rm_hos1=$1
rm_info=$2
rec_type=$3
input ()
{
if [ "$rec_type" == "a" ]; then
rm_info=$2
rec_type=$3
else
rec_type=$2
fi
I went off on a tangent of redoing my code due to a suggestion to use getopt and getopts commands. along with shift
That got a bit confusing to me so I decided to play around and see if some other IF statements would do it under the inputs section of my code.
I had it stuck in my head that I needed to "skip" input #2 which is what I wanted to do but went a different way of doing so.
not within this post but I had others suggest to use getopt and getopts commands.
Those are very good choices if you have more than a very few options to handle, want to offer option variants such as short and long versions or have options which require arguments.
getopts is a bash shell built-in and you can find detail in the bash man page.
getopt is a GNU utility (my personal favorite) which gives you support for long options, optional or required arguments, etc.
But to use either you will need to understand basic shell scripting which is not evident from the code fragments in your original post. As noted already, most of the code you posted is simply wrong and if some of the tests seem to be working then that is really just lucky, or perhaps unlucky coincidence.
The ABS might be a good place to begin learning bash shell scripting. Don't let the "Advanced" part scare you off, it is really a good guide. And I always suggest learning your way around the bash man page - it is long but that is because it is comprehensive, complete and well organized! Learn your way around it and it will become you best resource!
Before offering advice on how to fix your existing code it would be helpful to know what is in those positional arguments, $1, $2 and $3. Are they numbers, or strings of characters? Are they passed from the command line or is this inside a function or are they set earlier in the script? Are you testing the actual value or just testing whether they are set or not?
Last edited by astrogeek; 09-25-2021 at 03:22 PM.
Reason: ptoys
Those are very good choices if you have more than a very few options to handle, want to offer option variants such as short and long versions or have options which require arguments.
getopts is a bash shell built-in and you can find detail in the bash man page.
getopt is a GNU utility (my personal favorite) which gives you support for long options, optional or required arguments, etc.
But to use either you will need to understand basic shell scripting which is not evident from the code fragments in your original post. As noted already, most of the code you posted is simply wrong and if some of the tests seem to be working then that is really just lucky, or perhaps unlucky coincidence.
The ABS might be a good place to begin learning bash shell scripting. Don't let the "Advanced" part scare you off, it is really a good guide. And I always suggest learning your way around the bash man page - it is long but that is because it is comprehensive, complete and well organized! Learn your way around it and it will become you best resource!
Before offering advice on how to fix your existing code it would be helpful to know what is in those positional arguments, $1, $2 and $3. Are they numbers, or strings of characters? Are they passed from the command line or is this inside a function or are they set earlier in the script? Are you testing the actual value or just testing whether they are set or not?
what is in those positional arguments, $1, $2 and $3. Are they numbers, or strings of characters?
They could be numbers or characters
Are they passed from the command line or is this inside a function or are they set earlier in the script?
they are command-line when the script is ran on server manually
if the script is called by something else then there will be arguments being passed in for those inputs
Are you testing the actual value or just testing whether they are set or not?
$1 is a value that will not change after input
$2 is a value that could be updated after input
$3 is a value that will not change after input however the outcome, scenario depends on the input
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.