LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-29-2013, 05:33 AM   #1
111
LQ Newbie
 
Registered: Jun 2013
Posts: 5

Rep: Reputation: Disabled
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".

Last edited by 111; 06-29-2013 at 12:58 PM.
 
Old 06-29-2013, 08:48 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,544

Rep: Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898
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.
 
Old 06-29-2013, 05:27 PM   #3
111
LQ Newbie
 
Registered: Jun 2013
Posts: 5

Original Poster
Rep: Reputation: Disabled
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.
 
Old 06-29-2013, 07:44 PM   #4
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian Jessie / sid
Posts: 1,471

Rep: Reputation: 444Reputation: 444Reputation: 444Reputation: 444Reputation: 444
Quote:
Originally Posted by 111 View Post
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 )
 
1 members found this post helpful.
Old 07-01-2013, 04:09 AM   #5
111
LQ Newbie
 
Registered: Jun 2013
Posts: 5

Original Poster
Rep: Reputation: Disabled
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.

Last edited by 111; 07-01-2013 at 04:16 AM.
 
Old 07-01-2013, 04:16 AM   #6
TobiSGD
Moderator
 
Registered: Dec 2009
Location: Germany
Distribution: Whatever fits the task best
Posts: 17,142
Blog Entries: 2

Rep: Reputation: 4846Reputation: 4846Reputation: 4846Reputation: 4846Reputation: 4846Reputation: 4846Reputation: 4846Reputation: 4846Reputation: 4846Reputation: 4846Reputation: 4846
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
 
1 members found this post helpful.
Old 07-01-2013, 05:21 AM   #7
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,544

Rep: Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898
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?
 
Old 07-02-2013, 06:33 AM   #8
111
LQ Newbie
 
Registered: Jun 2013
Posts: 5

Original Poster
Rep: Reputation: Disabled
getopts sounds like it will do the trick - thanks for the suggestion.
 
Old 07-03-2013, 02:10 PM   #9
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,826

Rep: Reputation: 1973Reputation: 1973Reputation: 1973Reputation: 1973Reputation: 1973Reputation: 1973Reputation: 1973Reputation: 1973Reputation: 1973Reputation: 1973Reputation: 1973
Quote:
Originally Posted by Firerat View Post
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.
 
1 members found this post helpful.
Old 07-04-2013, 07:34 AM   #10
111
LQ Newbie
 
Registered: Jun 2013
Posts: 5

Original Poster
Rep: Reputation: Disabled
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.
 
  


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] How do you use environment variables in the .bashrc file... BMan8577 Linux - Newbie 3 04-17-2011 01:16 PM
setting shell variables- no .bashrc aquaboot Linux Mint 9 01-07-2011 09:33 AM
Environmental variables .bashrc Skillz Linux - General 3 06-21-2010 10:09 AM
function in .bashrc ovince Programming 6 04-25-2007 12:26 PM
argument variables in .bashrc's aliases? stabu Slackware 3 12-15-2006 11:30 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 01:32 AM.

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