LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-06-2018, 08:01 AM   #1
masavini
Member
 
Registered: Jun 2008
Posts: 240

Rep: Reputation: 6
bash and input type detection...


hi,
as far as i know there are 2 ways to pass input data to a bash function:

1) via standard input
Code:
  func_name < data.txt
  func_name <<< "${data}"
2) via positional parameters:
a. passing input data filename
Code:
  func_name "data.txt"
b. passing the data variable
Code:
  func_name "${data}"
c. passing the data var name and then declaring it by reference
Code:
  function func_name () {
    declare -n input_data=$1
    ...
  }
  func_name data

what is the best way to deal with all of these options, so that you can remember which input way should be used with each function you write?

would it be a good idea to write a helper function to detect the input type and pass it to the commands of the main function?

i mean something like this:

Code:
function main () {

  declare input=$(__input_detect "$@")
  
  cat ${input}

  return 0
}

function __input_detect () {

  # check if standard input is present
  if read -t0; then
    echo "< /dev/stdin"
    return 0
  fi
  
  [[ -z ${1+x} ]] \
    && retun 0
    
  # check if $1 is a valid file name
  if [[ -f $1 ]]; then
    echo "$1"
  # check if $1 is the name of an existing variable
  elif [[ ! -z ${!1+x} ]]; then
    echo "<<< \"${!1}\""
  else
    echo "<<< \"$1\""
  fi
  
  return 0

}

# all of these invocation ways should work:
main_func data.txt
main_func < data.txt
main_func <<< "${data}"

# and these should not (so far):
main_func "${data}"
main_func data

this example is only partially working, but i'm posting it just to let you understand what i mean...

do you think such an input detection is somehow useful, or is it just a waste of time?
 
Old 12-06-2018, 10:08 AM   #2
GazL
Senior Member
 
Registered: May 2008
Posts: 4,795
Blog Entries: 14

Rep: Reputation: Disabled
One common convention in UNIXland is to treat a filename of '-' as meaning read from stdin instead. Some programs may even default to this behavior when no arguments are specified.

I'd recommend you follow that design wherever it makes sense to do so.
 
Old 12-07-2018, 03:32 AM   #3
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 10,186
Blog Entries: 7

Rep: Reputation: 2520Reputation: 2520Reputation: 2520Reputation: 2520Reputation: 2520Reputation: 2520Reputation: 2520Reputation: 2520Reputation: 2520Reputation: 2520Reputation: 2520
i understand the question.

my question is:

why?

is there an actual problem that needs solving, or is this pure curiosity?
 
Old 12-07-2018, 04:02 AM   #4
MadeInGermany
Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 953

Rep: Reputation: 417Reputation: 417Reputation: 417Reputation: 417Reputation: 417
Can you simply do the following?
Code:
main(){
    cat "$@"
}
main "$@"
The "$@" passes the input properly:
the function or the cat run on the given arguments or from stdin if no arguments.
 
Old 12-09-2018, 04:09 PM   #5
masavini
Member
 
Registered: Jun 2008
Posts: 240

Original Poster
Rep: Reputation: 6
Quote:
Originally Posted by ondoho View Post
i understand the question.

my question is:

why?

is there an actual problem that needs solving, or is this pure curiosity?
curiosity, mostly...

i've always thought that stdin was the most convenient way to pass input to a program but found it very expensive when processing large data amounts.

so i found myself having most of the programs processing stdin only and some other processing input text files only, that made me sometimes wonder 'how does that work..?'

then i discovered how to pass variables by reference, started using it in some programs and the occasional indecision i mentioned before became pretty usual...

so the actual question: am i the only one in the world who can't remember how to properly invoke his own scripts? if not (as i hope), how others solved this? maybe by making their programs accepting any type of input? if so, how?

Last edited by masavini; 12-09-2018 at 04:22 PM.
 
Old 12-09-2018, 08:31 PM   #6
scasey
Senior Member
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.5
Posts: 2,204

Rep: Reputation: 688Reputation: 688Reputation: 688Reputation: 688Reputation: 688Reputation: 688
Quote:
Originally Posted by masavini View Post
so the actual question: am i the only one in the world who can't remember how to properly invoke his own scripts? if not (as i hope), how others solved this? maybe by making their programs accepting any type of input? if so, how?
I usually include a check of the input and a usage example...
Code:
($email, $ipaddr, $fwdmsg) = @ARGV;

if (!$email || !$ipaddr || !$fwdmsg) {
	print "usage is: sndspamrpt.pl <abuse email> <ip> <filename>\n";
	print " example: sndspamrpt.pl abuse\@somedomain.com 123.456.798.23 /home/postmaster/Maildir/db/new/msg.ABCD\n";
	exit;
}
So,when run without arguments we get
Code:
sndspamrpt.pl 
usage is: sndspamrpt.pl <abuse email> <ip> <filename>
 example: sndspamrpt.pl abuse@somedomain.com 123.456.798.23 /home/postmaster/Maildir/db/new/msg.ABCD
Do that when you create the script and it will remind you what to do.

Last edited by scasey; 12-09-2018 at 09:32 PM.
 
1 members found this post helpful.
Old 12-10-2018, 05:35 AM   #7
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,643

Rep: Reputation: 2961Reputation: 2961Reputation: 2961Reputation: 2961Reputation: 2961Reputation: 2961Reputation: 2961Reputation: 2961Reputation: 2961Reputation: 2961Reputation: 2961
Like scasey, a good usage message never goes astray and can be triggered by other the wrong number / type of arguments or maybe with a helper option such as -h or --help

I would add that everything passed to a function is actually read in as a string, ie when passing the name of a file to a function / script at the exact time of being called it is
not a file name but just a simple string. It would not be until it is used by something that reads / tests files that you would now if it were actually the name of a file
Code:
#-------------------------------------------------------------------------------
# Overview: Provide usage information for script
#
# Function:   run_instructions
#
# Parameters: NA
#
# Return:     usage message
#-------------------------------------------------------------------------------
run_instructions()
{
  cat<<-USAGE
   
    Usage: ${0##*/} [switch] [file|type]
  
    Switch:
        -d, --defaults        User can use default values
        -i, --info [type]     Request field information on type
        -t, --template [name] Create template install file
        -v, --verbose         Allows system commands to show verbose output, eg. chmod -v
        -h, --help            Display this help message
  
    File:
        file              Name of File List to use in script
        type              Type to be used in input file
        name              Name to be used for input file creation
  
    Aborted : $(date)
  
  USAGE

  exit 1
} #run_instructions
So the above is my standard format for a usage message and a simple test to get this up would be:
Code:
for opt
do
  case "$opt" in
    ...) #other options here
    -h|--help|*) run_instructions;;
  esac
done
You obviously need to process the other parameters which are not option / switches but you get the general idea
So you would be able to call your script as:
Code:
./your_script.sh -h
And get a listing of what is accepted as input
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
[SOLVED] stty: standard input: Input/output error [bash] DoME69 Programming 7 05-22-2015 01:14 AM
Ethernet bonding -- link layer detection failover isn't enough, smarter detection? pwn Linux - Networking 1 07-10-2011 11:42 PM
User input into Bash scripts and checking validity of user input?? helptonewbie Programming 8 07-07-2008 07:40 PM
Repeated "input: AT Translated Set 2 keyboard as /class/input/input" messages AcerKev Mandriva 2 09-16-2007 09:35 AM
my mouse input is takes as keyboard input in BASH e1000 Slackware 5 12-08-2003 04:00 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:03 AM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration