function with variables in .bashrc
I'm attempting to write a function with some variables in .bashrc.
Code:
function yv () Code:
[!] Invalid value <v> for option <orderby> also I'm editing .bashrc in vim and the "r" preceding "=rating" is a different color - the same as "alias" and "export". |
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. |
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' 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. |
Quote:
Code:
function yv () 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 The shell has executed this; Code:
youtube-viewer --orderby="v" --author="Firerat" Code:
yv v Firerat Code:
function yv () 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 ) |
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 () 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. |
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 |
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="" ... |
getopts sounds like it will do the trick - thanks for the suggestion.
|
Quote:
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. |
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. |