LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 07-28-2019, 06:10 PM   #1
GPGAgent
Member
 
Registered: Oct 2018
Location: Cornwall UK
Distribution: Mint 20 xfce 64bit
Posts: 719
Blog Entries: 3

Rep: Reputation: 122Reputation: 122
Question How to create a function


I use this code in my bash scripts to prompt for input:
Code:
default="60-80"
read -e -p "Gap in Seconds between cuts [${bold}$default${norm}]: " GAP_SPAN
GAP_SPAN=${GAP_SPAN:-$default}
How can I turn this into a function?

I tried this but it doesn't work:
Code:
# Usage: x=$( ReadInput "Output Extension [${bold}$default${norm}|mpg|avi]: " "*.mp4" )
function ReadInput { 
default="$2"
read -p "$1" "$2"
echo ${x:-$default}
}
And I called it later in the same script like this:
Code:
...
..
...
bold=$(tput bold)
norm=$(tput sgr0)

FF=$( ReadInput "Output Extension [${bold}$default${norm}|mpg|avi]: " "mp4" )
echo $FF
..
...
..
And also it doen't bold face the default value
 
Old 07-28-2019, 06:44 PM   #2
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,222
Blog Entries: 13

Rep: Reputation: 4276Reputation: 4276Reputation: 4276Reputation: 4276Reputation: 4276Reputation: 4276Reputation: 4276Reputation: 4276Reputation: 4276Reputation: 4276Reputation: 4276
You don't need the word function.

I have an example of functions in the same file as the main part of the script in the blog about bash programming in my signature. There is a section specifically about functions.
 
1 members found this post helpful.
Old 07-28-2019, 08:36 PM   #3
berndbausch
LQ Addict
 
Registered: Nov 2013
Location: Tokyo
Distribution: A few
Posts: 6,072

Rep: Reputation: 1889Reputation: 1889Reputation: 1889Reputation: 1889Reputation: 1889Reputation: 1889Reputation: 1889Reputation: 1889Reputation: 1889Reputation: 1889Reputation: 1889
Quote:
Originally Posted by GPGAgent View Post
I tried this but it doesn't work:
Can you be more specific? How did you test it? Does it issue an error message? In which way does it behave differently than expected?

Last edited by berndbausch; 07-28-2019 at 08:37 PM.
 
Old 07-28-2019, 10:32 PM   #4
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (current), FreeBSD, Win10, It varies
Posts: 9,952

Rep: Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148
Quote:
Originally Posted by GPGAgent View Post
I use this code in my bash scripts to prompt for input:
Code:
default="60-80"
read -e -p "Gap in Seconds between cuts [${bold}$default${norm}]: " GAP_SPAN
GAP_SPAN=${GAP_SPAN:-$default}
How can I turn this into a function?

I tried this but it doesn't work:
Code:
# Usage: x=$( ReadInput "Output Extension [${bold}$default${norm}|mpg|avi]: " "*.mp4" )
function ReadInput { 
default="$2"
read -p "$1" "$2"
echo ${x:-$default}
}
And I called it later in the same script like this:
Code:
...
..
...
bold=$(tput bold)
norm=$(tput sgr0)

FF=$( ReadInput "Output Extension [${bold}$default${norm}|mpg|avi]: " "mp4" )
echo $FF
..
...
..
And also it doen't bold face the default value
minus some error checking and all the bells and whistles.
Code:
#!/bin/bash

usage()
{
	echo "
	Enter two prams in seconds 
	$0 <pram> <pram> "
	
}
getPrams()
{
	
	[[ "$#" -ne 2 ]] &&
	{ echo " I need two prams yo!" ; usage ;  exit ; }
	
	GAP_SPAN=$1-$2
}

getPrams2()
{
	GAP_SPAN="GAP_SPAN=$sec1-$sec2"
}

getPrams $1 $2

echo "GAP_SPAN=$GAP_SPAN"

read -p "I need two prams for seconds " sec1 sec2

echo "$sec1 $sec2"

getPrams2

echo $GAP_SPAN
Two different ways to get information off the CLI and pass it into a function.
should give you an Idea of something.

Last edited by BW-userx; 07-28-2019 at 10:44 PM.
 
2 members found this post helpful.
Old 07-29-2019, 04:01 AM   #5
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 15,948

Rep: Reputation: 5279Reputation: 5279Reputation: 5279Reputation: 5279Reputation: 5279Reputation: 5279Reputation: 5279Reputation: 5279Reputation: 5279Reputation: 5279Reputation: 5279
interesting (at least for me): using this small script:
Code:
#!/bin/bash

bold="$(tput bold)"
norm="$(tput sgr0)"

function ReadInput { 
    default="$2"
    read -p "$1" "$2"
    echo ${x:-$default}
}

ReadInput "Output Extension [${bold}$default${norm}|mpg|avi]: " "mp4"
ReadInput "Output Extension [${bold}$default${norm}|mpg|avi]: " "mp4"
I will get the following result:
Code:
user@host:/tmp$ ./a.sh
Output Extension [|mpg|avi]: 
mp4
Output Extension [mp4|mpg|avi]: 
mp4
user@host:/tmp$
that means the first invocation did not work properly, but the second.

I don't know if this is the original issue (OP should tell us) and if it was reproducible at all.
 
Old 07-29-2019, 09:29 AM   #6
GPGAgent
Member
 
Registered: Oct 2018
Location: Cornwall UK
Distribution: Mint 20 xfce 64bit
Posts: 719

Original Poster
Blog Entries: 3

Rep: Reputation: 122Reputation: 122
Quote:
Originally Posted by berndbausch View Post
Can you be more specific? How did you test it? Does it issue an error message? In which way does it behave differently than expected?
Ahh, yes I should have said how it doesn't work - here goes:
Code:
charlie@charlie-machine:~$ 000-Skeleton.sh 
10761 (process ID) old priority 0, new priority 19
 from function - Output Extension [|mpg|avi]: MP4
FF: mp4 <=== Output here
from body - Output Extension [mp4|mpg|avi]: MP4
OUTPUTEXTENSION: MP4 <=== Output here
See the attached image as well

1 The function doesn't display the default value - it should be mp4 and in bold face

2 I entered MP4, the function returned mp4

The code in the body performs as I expected
Attached Thumbnails
Click image for larger version

Name:	FF.png
Views:	5
Size:	21.0 KB
ID:	30978  
 
Old 07-29-2019, 09:40 AM   #7
GPGAgent
Member
 
Registered: Oct 2018
Location: Cornwall UK
Distribution: Mint 20 xfce 64bit
Posts: 719

Original Poster
Blog Entries: 3

Rep: Reputation: 122Reputation: 122
Quote:
Originally Posted by pan64 View Post
interesting (at least for me): using this small script:
Code:
#!/bin/bash

bold="$(tput bold)"
norm="$(tput sgr0)"

function ReadInput { 
    default="$2"
    read -p "$1" "$2"
    echo ${x:-$default}
}

ReadInput "Output Extension [${bold}$default${norm}|mpg|avi]: " "mp4"
ReadInput "Output Extension [${bold}$default${norm}|mpg|avi]: " "mp4"
I will get the following result:
Code:
user@host:/tmp$ ./a.sh
Output Extension [|mpg|avi]: 
mp4
Output Extension [mp4|mpg|avi]: 
mp4
user@host:/tmp$
that means the first invocation did not work properly, but the second.

I don't know if this is the original issue (OP should tell us) and if it was reproducible at all.
I tried that and this is what resulted:

The code:
Code:
#!/bin/bash
renice 19 -p $$

# Usage: x=$( ReadInput "Output Extension [${bold}$default${norm}|mpg|avi]: " "*.mp4" )
function ReadInput { 
default="$2"
read -p "$1" "$2"
echo ${x:-$default}
}


bold=$(tput bold)
norm=$(tput sgr0)

FF=$( ReadInput " from function - Output Extension [${bold}$default${norm}|mpg|avi]: " "mp4" )
echo "FF: $FF <=== Output here"

FF=$( ReadInput " from function - Output Extension [${bold}$default${norm}|mpg|avi]: " "mp4" )
echo "FF: $FF <=== Output here"

default="mp4"
read -p "from body - Output Extension [${bold}$default${norm}|mpg|avi]: " OUTPUTEXTENSION
OUTPUTEXTENSION=${OUTPUTEXTENSION:-$default}
echo "OUTPUTEXTENSION: $OUTPUTEXTENSION <=== Output here"

exit 99
Code:
charlie@charlie-machine:~$ 000-Skeleton.sh 
11294 (process ID) old priority 0, new priority 19
 from function - Output Extension [|mpg|avi]: 99
FF: mp4 <=== Output here
 from function - Output Extension [|mpg|avi]: 99
FF: mp4 <=== Output here
from body - Output Extension [mp4|mpg|avi]: 99
OUTPUTEXTENSION: 99 <=== Output here
charlie@charlie-machine:~$
and see attached image

Thanks for looking
The output:
Attached Thumbnails
Click image for larger version

Name:	FFF.png
Views:	8
Size:	24.1 KB
ID:	30979  
 
Old 07-29-2019, 09:54 AM   #8
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 15,948

Rep: Reputation: 5279Reputation: 5279Reputation: 5279Reputation: 5279Reputation: 5279Reputation: 5279Reputation: 5279Reputation: 5279Reputation: 5279Reputation: 5279Reputation: 5279
yes, that is a bit different. I don't understand it at this moment, but will try to find an explanation.
 
1 members found this post helpful.
Old 07-29-2019, 10:35 AM   #9
GPGAgent
Member
 
Registered: Oct 2018
Location: Cornwall UK
Distribution: Mint 20 xfce 64bit
Posts: 719

Original Poster
Blog Entries: 3

Rep: Reputation: 122Reputation: 122
Quote:
Originally Posted by rtmistler View Post
You don't need the word function.
I know, I just prefer it this way, and great blog as well
 
  


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
c++: create a function within a function? stateless Programming 3 01-30-2013 09:39 PM
[SOLVED] Threaded function cannot call a function with extern "C" but nonthreaded function can morty346 Programming 16 01-12-2010 06:00 PM
A main can be changed by a function local without passing anything to the function? ananthbv Programming 10 05-04-2004 02:31 PM
Is the wait function is the same as the sleep function ? Linh Programming 3 04-28-2004 01:39 PM
Perl exec function in linux (and system-function) nazula Programming 1 04-19-2004 01:21 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 02:55 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
Open Source Consulting | Domain Registration