LinuxQuestions.org

LinuxQuestions.org (http://www.linuxquestions.org/questions/index.php)
-   Linux - General (http://www.linuxquestions.org/questions/forumdisplay.php?f=1)
-   -   Bash (http://www.linuxquestions.org/questions/showthread.php?t=4175456201)

Spazztic_Killer 03-30-2013 06:00 PM

Bash
 
I have a question, is there a way to call or pull $1,$2 etc can me called from within a function.

lleb 03-30-2013 06:05 PM

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.

Spazztic_Killer 03-30-2013 06:47 PM

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

jpollard 04-01-2013 07:47 AM

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...

Spazztic_Killer 04-01-2013 03:23 PM

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");


Spazztic_Killer 04-01-2013 03:26 PM

Quote:

Originally Posted by Spazztic_Killer (Post 4922999)
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.

chrism01 04-02-2013 01:56 AM

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.

Spazztic_Killer 04-05-2013 08:24 PM

Thank you for explaining what was what, and gave me examples which helped thank you again


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