LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
Search this Thread
Old 03-30-2013, 06:00 PM   #1
Spazztic_Killer
Member
 
Registered: Dec 2012
Distribution: CentOS
Posts: 38

Rep: Reputation: Disabled
Bash


I have a question, is there a way to call or pull $1,$2 etc can me called from within a function.
 
Old 03-30-2013, 06:05 PM   #2
lleb
Senior Member
 
Registered: Dec 2005
Location: Florida
Distribution: CentOS/Fedora
Posts: 2,526

Rep: Reputation: 448Reputation: 448Reputation: 448Reputation: 448Reputation: 448
what do you mean, you are not clear. $1 and $2 can be many things depending on what is going on. Yes you can call variables within a function in bash.
 
Old 03-30-2013, 06:47 PM   #3
Spazztic_Killer
Member
 
Registered: Dec 2012
Distribution: CentOS
Posts: 38

Original Poster
Rep: Reputation: Disabled
What I mean, is that I'm dealing with a simple script that I want to test whether or not the user used $1, $2, etc as positional parameters to the script name. But I want to do the test inside of a function. Like so,
Code:
func_name(){
if [ "${?}" -eq 0 ] && [ -n "${1}" ] && [ -n "${2}" ] || [ -n "${3}" ]; # Check, if parameters are present.
  then
    UN="${1}" # Set Variable.
    CMT="${2} ${3}" # Set Variable.
  elif [ "${?}" -ne 0 ] && [ -z "${1}" ] || [ -z "${2}" ]; # Check, if parameters aren't present.
    then
      read -p "Enter a username: " UN # Set Variable.
      read -p "Enter a fullname: " CMT # Set Variable.
        if [ "${?}" -eq 0 ] && [ -n "${UN}" ] && [ -n "${CMT}" ]; # Check  if variables, are set.
          then
            echo > /dev/null # Send to bit bucket.
          else
            printf "%s\n" "Both a Username and Fullname Required." # If error, print message.
            exit 1
        fi
  else
    printf "%s\n" "Variables didn't set." # If error, print message.
    exit 1
fi
}
func_name
It doesn't even seem like it see the positional parameters
 
Old 04-01-2013, 07:47 AM   #4
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 2,153

Rep: Reputation: 554Reputation: 554Reputation: 554Reputation: 554Reputation: 554Reputation: 554
It doesn't. The context of the function gets its own version of the parameters. Since the function has no parameters, there aren't any to be evaluated.

These parameters are always just parameters, they are not variables that can be used for just anything - their context must also be considered.

What you COULD do, is assign them to internal variables (or an array) and then evaluate those variables/array. To the internal bash function, they would be globals...
 
Old 04-01-2013, 03:23 PM   #5
Spazztic_Killer
Member
 
Registered: Dec 2012
Distribution: CentOS
Posts: 38

Original Poster
Rep: Reputation: Disabled
I have been doing some work on learning array's but, I have not figured it out yet. I mean it dones work but not sure how I guess work it into a script/cmd. My orignal ideal was for this to test wether or not the user existed by my array that i could call or display the var. And it would display all users in the /etc/password file with the usersnames single quoted.
Code:
 #_users=$(cat /etc/passwd | cut -d':' -f1 | )
#declare -a _users=$(awk -F':' '{print $1}' /etc/passwd | sed "s/^/\'/g" | sed "s/$/\'/g");

Last edited by Spazztic_Killer; 04-01-2013 at 03:31 PM.
 
Old 04-01-2013, 03:26 PM   #6
Spazztic_Killer
Member
 
Registered: Dec 2012
Distribution: CentOS
Posts: 38

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Spazztic_Killer View Post
I have been doing some work on learning array's but, I have not figured it out yet. I mean it dones work but not sure how I guess work it into a script/cmd. My orignal ideal was for this to test wether or not the user existed by my array that i could call or display the var. And it would display all users in the /etc/password file with the usersnames single quoted.
Code:
#declare -a _users=$(awk -F':' '{print $1}' /etc/passwd | sed "s/^/\'/g" | sed "s/$/\'/g");
Wasnt sure how to call each username individually as an array.

Last edited by Spazztic_Killer; 04-01-2013 at 03:32 PM.
 
Old 04-02-2013, 01:56 AM   #7
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,261

Rep: Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028
As above, the fn gets its own private list of params $1, $2 etc.
You can pass the 'main' $1, $2 directly, or as named params
Code:
# option 1
fn_name ()
{
.
.
}
# passing 'main' aka 'global' params directly
fn_name $1 $2

# option 2
fn_name ()
{
.
.
}
# passing 'main' aka 'global' params as named params
v1=$1
v2=$2
fn_name  $v1 $v2
Personally I prefer the latter (named params) even though in this case it could be considered redundant.
 
Old 04-05-2013, 08:24 PM   #8
Spazztic_Killer
Member
 
Registered: Dec 2012
Distribution: CentOS
Posts: 38

Original Poster
Rep: Reputation: Disabled
Thank you for explaining what was what, and gave me examples which helped thank you again
 
  


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 On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: Share And Discover Cool Bash Tricks With Bash One-Liners LXer Syndicated Linux News 0 01-30-2012 09:50 AM
Bash problem : -bash: [: /bin/bash: unary operator expected J.A.X Linux - Software 1 09-22-2011 05:52 AM
[SOLVED] Using a long Bash command including single quotes and pipes in a Bash script antcore Linux - General 9 07-22-2009 11:10 AM
BASH -copy stdin to stdout (replace cat) (bash browser) gnashley Programming 4 07-21-2008 01:14 PM
why did bash 2.05b install delete /bin/bash & "/bin/sh -> bash"? johnpipe Linux - Software 2 06-06-2004 06:42 PM


All times are GMT -5. The time now is 11:57 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration