Bash
I have a question, is there a way to call or pull $1,$2 etc can me called from within a function.
|
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.
|
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(){ |
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... |
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 | ) |
Quote:
|
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 |
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 06:02 AM. |