LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices


Reply
  Search this Thread
Old 09-24-2021, 05:58 PM   #1
CyberIT
Member
 
Registered: Jun 2017
Posts: 56

Rep: Reputation: Disabled
Question if...else...statement


Im stuck on this if...else...statement.

Code:
usage ()
usage1 ()
usage2 ()

update ()
{
rm_hos1=$1
rm_info=$2
rec_type=$3

input ()
{
if [ rm_hos1=$1 ] && [ rm_info=$2 ]; then
  if [ $# -ne 2 ]; then
    usage
  fi

elif [ rm_hos1=$1 ] && [ rm_info=$2 ] && [ rec_type=$3 ]; then
  if [ $# -ne 3 ]; then
    usage2
  fi

elif [ rm_hos1=$1 ] && [ rec_type=$2 ]; then
  if [ $# -ne 2 ]; then
    usage1
  fi
fi
}
Code:
if [ "$rec_type" == "a" ]; then
  ...command
  ...command

elif [ "$rec_type" == "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!

Last edited by CyberIT; 09-25-2021 at 03:31 PM.
 
Old 09-24-2021, 10:36 PM   #2
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,703

Rep: Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896
Whitespace is important with if statements. Compare the second code block with yours and use shellcheck.net to check your syntax.
 
1 members found this post helpful.
Old 09-25-2021, 07:01 AM   #3
boughtonp
Senior Member
 
Registered: Feb 2007
Location: UK
Distribution: Debian
Posts: 3,601

Rep: Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546

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.


Last edited by boughtonp; 09-25-2021 at 07:02 AM.
 
1 members found this post helpful.
Old 09-25-2021, 08:24 AM   #4
CyberIT
Member
 
Registered: Jun 2017
Posts: 56

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by michaelk View Post
Whitespace is important with if statements. Compare the second code block with yours and use shellcheck.net to check your syntax.
Thank you! I will do that and update...
 
Old 09-25-2021, 08:26 AM   #5
CyberIT
Member
 
Registered: Jun 2017
Posts: 56

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by boughtonp View Post
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
 
Old 09-25-2021, 10:44 AM   #6
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053
Quote:
Originally Posted by CyberIT View Post
Im stuck on this if...else...statement.

Code:
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.
 
Old 09-25-2021, 02:48 PM   #7
CyberIT
Member
 
Registered: Jun 2017
Posts: 56

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by ondoho View Post
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.
Why would my code in first block be wrong?

In the end, Im trying to achieve this...

by using these inputs

rm1=$1
rm2=$2
rec1=$3

First IF statement receives inputs 1 and 2. completes successfully

Second IF statement needs inputs 1 and 2 and 3. completes successfully

Third IF statement needs inputs 1 and 3 but not 2. completes successfully

Last edited by CyberIT; 09-25-2021 at 03:04 PM.
 
Old 09-25-2021, 02:51 PM   #8
CyberIT
Member
 
Registered: Jun 2017
Posts: 56

Original Poster
Rep: Reputation: Disabled
not within this post but I had others suggest to use getopt and getopts commands.
 
Old 09-25-2021, 03:00 PM   #9
CyberIT
Member
 
Registered: Jun 2017
Posts: 56

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by michaelk View Post
Whitespace is important with if statements. Compare the second code block with yours and use shellcheck.net to check your syntax.
Whitespace was pretty much the issue here. Comparing the two blocks of code, I ended up updating these lines


Code:
if [ rm_hos1 = $1 ] && [ rm_info = $2 ]; then
Code:
elif [ rm_hos1 = $1 ] && [ rm_info = $2 ] && [ rec_type = $3 ]; then
Code:
elif [ rm_hos1 = $1 ] && [ rec_type = $2 ]; then
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.

Last edited by CyberIT; 09-25-2021 at 03:36 PM.
 
Old 09-25-2021, 03:06 PM   #10
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,264
Blog Entries: 24

Rep: Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195
Quote:
Originally Posted by CyberIT View Post
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
 
Old 09-25-2021, 03:46 PM   #11
CyberIT
Member
 
Registered: Jun 2017
Posts: 56

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by astrogeek View Post
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

Last edited by CyberIT; 09-25-2021 at 03:48 PM.
 
Old 09-25-2021, 03:53 PM   #12
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,703

Rep: Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896
Quote:
what is in those positional arguments, $1, $2 and $3. Are they numbers, or strings of characters?
They could be numbers or characters
Yes...
https://tecadmin.net/tutorial/bash-s...and-arguments/
 
  


Reply

Tags
bash, conditionals, linux



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Running mysql from ssh & query statement has a Text Header in the SELECT statement? djlerman Linux - Server 6 11-19-2013 06:33 PM
Perl switch statement throwing error like Bad case statement (invalid case value?) kavil Programming 2 10-07-2010 04:50 AM
[SOLVED] Shell script for adding a statement in a file after a particular statement Aquarius_Girl Programming 4 06-28-2010 03:07 AM
Problem with if statement in a find -exec statement romsieze Programming 2 10-02-2008 12:38 AM
Case statement with If statement cbo0485 Linux - Newbie 4 11-07-2007 08:05 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Server

All times are GMT -5. The time now is 05:24 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration