LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   function with variables in .bashrc (https://www.linuxquestions.org/questions/linux-newbie-8/function-with-variables-in-bashrc-4175467804/)

111 06-29-2013 05:33 AM

function with variables in .bashrc
 
I'm attempting to write a function with some variables in .bashrc.

Code:

function yv ()
{
p=published
r=rating
v=viewCount

youtube-viewer --orderby="$1" --author="$2"
}

here's the error mess I get:
Code:

[!] Invalid value <v> for option <orderby>
I've tried various combinations of the above funcion using "local", adding extra "$" and brackets but nothing has worked so far.

also I'm editing .bashrc in vim and the "r" preceding "=rating" is a different color - the same as "alias" and "export".

grail 06-29-2013 08:48 AM

Here are a few things I noticed:

1. Function is spelt wrong ... maybe typo

2. Variables are never used ... could be a later addition

3. You do not show how you are calling the function??

Lastly, please use [code][/code] tags when showing data or code to preserver formatting and make it clearer.

111 06-29-2013 05:27 PM

basically what I'm trying to do is create a function or script of some sort that would simplify writing out various parameters such as "viewCount" and use a single letter instead like "v". Also it would allow for parameter combinations that would otherwise have to be written in numerous alias/functions.

for example

Code:

alias ylv='youtube-viewer --duration=long --orderby=viewCount'
alias ysr='youtube-viewer --duration=short --orderby=rating'

again like I say each of these could just be written out as an alias/function - and this is how I've done it so far but there should be another way to do this.

I know this explanation sounds confusing, but this the best way I can describe it.

about variables - they seem to be listed in all the bash programming guides, but I guess that may not even pertain to this situation.

also please note I have very little knowledge of scripting at this point.
and yes the "function" spelling was a typo.

Firerat 06-29-2013 07:44 PM

Quote:

Originally Posted by 111 (Post 4980930)
basically what I'm trying to do is create a function or script of some sort that would simplify writing out various parameters such as "viewCount" and use a single letter instead like "v". Also it would allow for parameter combinations that would otherwise have to be written in numerous alias/functions.

for example

Code:

alias ylv='youtube-viewer --duration=long --orderby=viewCount'
alias ysr='youtube-viewer --duration=short --orderby=rating'

again like I say each of these could just be written out as an alias/function - and this is how I've done it so far but there should be another way to do this.

I know this explanation sounds confusing, but this the best way I can describe it.

about variables - they seem to be listed in all the bash programming guides, but I guess that may not even pertain to this situation.

also please note I have very little knowledge of scripting at this point.
and yes the "function" spelling was a typo.

Code:

function yv ()
{
p=published
r=rating
v=viewCount

youtube-viewer --orderby="$1" --author="$2"
}

I think I understand, maybe ( CH3CH2OH )
Above is your original, ( mostly for me to see while I type )
You want a command that you can type like this
Code:

yv v Firerat
First reason that ain't going to work is .. I don't do YouTube ;)

The shell has executed this;
Code:

youtube-viewer --orderby="v" --author="Firerat"
Do you see what happened?
Code:

yv  v    Firerat
cmd  $1  $2

This probably fits better
Code:

function yv ()
{
case $1 in
    p) Orderby=published;;
    r) Orderby=rating;;
    v) Orderby=viewCount;;
    *) echo "Ehh? wtf Yow want?"
      echo "Valid options are :"
      echo "p, r and v"
      echo "You work out what they mean11"
      exit 234 # document 234 to be user screwed up;;
esac
Author=$2
# esac is case backwards ( and I still hate typing it )

youtube-viewer --orderby="$Orderby" --author="$Author"
}

Now, this ain't perfect by any standard
But it will do what you want ( I hope, not tested )
case is really best suited to when you need lots of nested if, else
But as you can see can be used to make a basic Option processor

getopt will be of interest
and case can be more elaborate than the above example

To be honest, I would probably stick something like that into its own standalone script ( in ~/bin/ for example )
And I would Name it a little more descriptive,
yv ?
YouTubeViewer, yesh I know what that is and does.

by the time you have typed
Y<tab>
you will have probably finished it already ;) ( look up tab completion )

111 07-01-2013 04:09 AM

ok that worked perfectly!

but I have a few questions:

What if I want to add another variable like "--duration=", here's what I came up with. I don't know if it's technically correct but it works:
Code:

function yv ()
{
case $1 in
    s) Duration=short;;
    m) Duration=medium;;
    l) Duration=long;;
esac
case $2 in
    p) Orderby=published;;
    r) Orderby=rating;;
    v) Orderby=viewCount;;
    *) echo "Ehh? wtf Yow want?"
      echo "Valid options are :"
      echo ""
      echo "duration"
      echo "========"
      echo " s - short"
      echo " m - medium"
      echo " l - long"
      echo ""
      echo "orderby"
      echo "======="
      echo " p - published"
      echo " r - rating"
      echo " v - viewCount"
      echo ""
      exec bash
      exit
esac
Author=$3
# esac is case backwards ( and I still hate typing it )

youtube-viewer --duration="$Duration" --orderby="$Orderby" --author="$Author"
}


Also since there are now three variables lets say I want to only use one or two variables rather than all three - is it possible to have "duration" recognize p,r,v as the first variable?

In other words maybe I don't want to search for "duration" this time - can I make it so that the first variable will be eliminated or substituted for the second and accept the variables "p,r,v" instead of "s,m,l". then the subsequent variable will automatically be author - or something like that...

Right now I just used another function - maybe that's just easier - I don't know.

and my last question:
When i first ran the script without variables - just the "yv" it would just exit the program.
Now it exits right out of the terminal so I had to add "exec bash" to the line preceding exit - again it works but I don't if that's correct.

but thanks for the reply - that's basically what I was looking for.

TobiSGD 07-01-2013 04:16 AM

If you want to have options that are not every time existing it is better to use getopts instead of hardcoding the parsing of the options.
Have a look here for a short description: http://wiki.bash-hackers.org/howto/getopts_tutorial

grail 07-01-2013 05:21 AM

You may also wish to consider what your command, youtube-viewer, thinks when it receives a blank option, ie if duration is not entered and the command you submit is:
Code:

youtube-viewer --duration="" ...
Does the above cause an error or is it happy to have blank information?

111 07-02-2013 06:33 AM

getopts sounds like it will do the trick - thanks for the suggestion.

David the H. 07-03-2013 02:10 PM

Quote:

Originally Posted by Firerat (Post 4980950)
Code:

    *) echo "Ehh? wtf Yow want?"
      echo "Valid options are :"
      echo "p, r and v"
      echo "You work out what they mean11"
      exit 234 # document 234 to be user screwed up;;


You probably don't want to use exit here. That will either terminate the entire script, or the current shell session if configured for use in an interactive shell. Use return instead to terminate the function only.

And always define all in-function variables as local, if you don't need to use them outside of the function. Otherwise you're likely to try to use the same variable name somewhere else and get a conflict. I've experienced several head-scratchers caused by this.

Finally, if you have multiple options to configure at once, use an array to store them, as shown here.

111 07-04-2013 07:34 AM

I used "return" instead of "exit" and it worked the way you mentioned. Also just looked at the link on arrays and will try this method - if I have any questions on this or getopts I'll post them.
thanks for the info.


All times are GMT -5. The time now is 04:16 AM.