LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-13-2023, 12:31 PM   #1
Faki
Member
 
Registered: Oct 2021
Posts: 574

Rep: Reputation: Disabled
Function for use as conditional


I want to write a bash function that tests whether a number or array values fall within the 8-bit possible values.

I want to use it as a conditional about whether all array elements are valid. The difficulty is setting up the exit status correctly when at least one value is outside range.

Code:
octxt ()
 {
  typeset -n _octary="$1"

  local xst
  for i in "${_octary[@]}"; do
    if (( i < 0 || i > 255 )); then
      xst=1  # Fail Value outside 8-Bit extent
    else
      xst=0  # Success Value within 8-Bit extent
    fi
  done

  return $xst

 }

Last edited by Faki; 04-13-2023 at 12:41 PM.
 
Old 04-13-2023, 02:14 PM   #2
metaed
Member
 
Registered: Apr 2022
Location: US
Distribution: Slackware64 15.0
Posts: 371

Rep: Reputation: 172Reputation: 172
Something like this?

Code:
octxt ()
 {
  typeset -n _octary="$1"

  local xst
  xst=0
  for i in "${_octary[@]}"; do
    if (( i < 0 || i > 255 )); then
      xst=1  # Fail Value outside 8-Bit extent
    fi
  done

  return $xst

 }
 
Old 04-15-2023, 08:50 AM   #3
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,818

Rep: Reputation: 1211Reputation: 1211Reputation: 1211Reputation: 1211Reputation: 1211Reputation: 1211Reputation: 1211Reputation: 1211Reputation: 1211
Return as soon as possible
Code:
octxt (){
  local -n _octary="$1"
  local i
  for i in "${_octary[@]}"; do
    (( i >= 0 && i <= 255 )) || return  # Fail Value outside 8-Bit extent
  done
}
Knowing that the last exit status is returned.
 
Old 04-15-2023, 09:14 AM   #4
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,996

Rep: Reputation: 7338Reputation: 7338Reputation: 7338Reputation: 7338Reputation: 7338Reputation: 7338Reputation: 7338Reputation: 7338Reputation: 7338Reputation: 7338Reputation: 7338
yes, return as soon as possible, when you know it is not valid.
But unfortunately:
Code:
a="adfgarfg"
(( a >= 0 )) && echo ok
(( a <= 255 )) && echo ok
It won't work for non-numerical inputs and also may produce strange results (for example if a="12w")
bash is not really good at it.
 
  


Reply

Tags
bash



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
LXer: How To Use When Conditional Statements In Ansible Playbooks LXer Syndicated Linux News 0 06-02-2022 06:57 AM
[SOLVED] numeric conditional on second argument of bash function Faki Linux - Newbie 12 05-10-2022 06:06 AM
[SOLVED] Threaded function cannot call a function with extern "C" but nonthreaded function can morty346 Programming 16 01-12-2010 05:00 PM
How to use conditional #define? kranti Programming 3 09-20-2006 09:06 AM
Can I get a socket from a function and use it on another function? elmafiacs Programming 2 10-14-2005 10:26 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 01:12 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