LinuxQuestions.org
Review your favorite Linux distribution.
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 03-13-2016, 06:13 PM   #1
lamont0
LQ Newbie
 
Registered: Feb 2016
Posts: 20

Rep: Reputation: Disabled
Creating a function


I create the file .whoisthere. At the command line I enter . .whoisthere2. Then I execute whoisthere2 "These are the folks who are logged in." The parameter $1 is not echoed as I expect. Tried with and without quotes. What am I failing to do?


Code:
# !/bin/bash
whoisthere2()
{
echo "$1"
who
}
 
Old 03-13-2016, 06:30 PM   #2
hydrurga
LQ Guru
 
Registered: Nov 2008
Location: Pictland
Distribution: Linux Mint 21 MATE
Posts: 8,048
Blog Entries: 5

Rep: Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925
Bash functions: http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO-8.html

But in fact you don't need a function. All you need to do is create the script whoisthere and then call it from the command line.

Give it another go.
 
Old 03-13-2016, 06:50 PM   #3
lamont0
LQ Newbie
 
Registered: Feb 2016
Posts: 20

Original Poster
Rep: Reputation: Disabled
Thank you for your reply. I have performed this with a script as shown by the example you provided. However, I want to learn how to do this by loading it into memory. When in memory it does no accept the argument that is passed but it will with the script. I do not understand the difference. What is needed when the function is loaded into memory to accept the arguement?
 
Old 03-13-2016, 06:55 PM   #4
hydrurga
LQ Guru
 
Registered: Nov 2008
Location: Pictland
Distribution: Linux Mint 21 MATE
Posts: 8,048
Blog Entries: 5

Rep: Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925
Quote:
Originally Posted by lamont0 View Post
Thank you for your reply. I have performed this with a script as shown by the example you provided. However, I want to learn how to do this by loading it into memory. When in memory it does no accept the argument that is passed but it will with the script. I do not understand the difference. What is needed when the function is loaded into memory to accept the arguement?
What do you mean by "loading it into memory"?

Do you mean saving the script as a file so that you can call it from the command line?
 
Old 03-13-2016, 07:19 PM   #5
lamont0
LQ Newbie
 
Registered: Feb 2016
Posts: 20

Original Poster
Rep: Reputation: Disabled
Loading the function into memory is performed by executing the following on the command line: . .whoisthere2. That is a period - space - period function name. The function is now in memory where it will now be executed.
 
Old 03-13-2016, 07:27 PM   #6
lamont0
LQ Newbie
 
Registered: Feb 2016
Posts: 20

Original Poster
Rep: Reputation: Disabled
I have to apologize. The function is now working as expected. Now sure what I did wrong but I am sure it was my fault. Sorry!
 
Old 03-13-2016, 07:39 PM   #7
hydrurga
LQ Guru
 
Registered: Nov 2008
Location: Pictland
Distribution: Linux Mint 21 MATE
Posts: 8,048
Blog Entries: 5

Rep: Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925
Quote:
Originally Posted by lamont0 View Post
I have to apologize. The function is now working as expected. Now sure what I did wrong but I am sure it was my fault. Sorry!
And my apologies for not quite understanding what you wanted.

If it's of any interest, this thread points out an alternative way of calling a script function directly (the highest voted answer):

http://stackoverflow.com/questions/8...n-command-line
 
  


Reply



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] problem creating an array from a function in bash mia_tech Programming 2 03-21-2014 09:17 AM
creating a strleading function in php UnixCube Programming 6 04-29-2012 08:55 PM
Need help creating a mail function.... gobeavs2009 Linux - Newbie 3 12-02-2009 07:05 PM
Problem when use getc function before creating a new process with execve() anuj_sri Linux - Newbie 1 02-20-2008 08:29 PM
C/SDL; creating my own drawLine function TheLinuxDuck Programming 4 09-20-2002 10:34 AM

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

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