LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 09-17-2019, 02:13 PM   #1
finalturismo
Member
 
Registered: May 2019
Posts: 41

Rep: Reputation: Disabled
[Request] .bashrc profile search youtube user input in URL, remove space HELP


.bashrc example help

alias youtube="chromium https://www.youtube.com/results?search_query=$@"

Can anyone help me with this

it is adding a space in the url and causing chromium to open another tab

from my understanding $@ is supposed to insert user input from terminal at the @ symbol, problem is, it is adding a space before the @.
 
Old 09-17-2019, 02:56 PM   #2
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian sid
Posts: 2,320

Rep: Reputation: 635Reputation: 635Reputation: 635Reputation: 635Reputation: 635Reputation: 635
not with alias, you want a function

I have speperarte files for alias and functions

~/.bash_alias
~/.bash_functions

and in ~/.bashrc
Code:
for i in ~/.bash_alias ~/.bash_functions
do
    [[ -e $i ]] && . $i
# note the --------^
# is is short for "source"
done

then in ~/.bash_functions
Code:
youtube () {
    chromium "https://www.youtube.com/results?search_query=${@// /+}"
}
I added a little bit to replace all the spaces with +
you might need to use %20 instead of + , I haven't checked

Edit spot the last min error
see later post
https://www.linuxquestions.org/quest...7/#post6037661

Last edited by Firerat; 09-17-2019 at 03:23 PM.
 
Old 09-17-2019, 03:03 PM   #3
finalturismo
Member
 
Registered: May 2019
Posts: 41

Original Poster
Rep: Reputation: Disabled
can you break down the meaning of ${@// /+}

I understand the if and, else statements and pipes etc..

its just i don't understand all the weird bash symbols yet. I am trying to graduate from bash grade school here and it is not easy.
 
Old 09-17-2019, 03:11 PM   #4
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian sid
Posts: 2,320

Rep: Reputation: 635Reputation: 635Reputation: 635Reputation: 635Reputation: 635Reputation: 635
I should probably explain why alias wasn't working

Code:
foo=foo
alias example="echo _$@_${foo}_"
Code:
example barfoo foobar
$@ is special, it is the array of all the arguments passed to the shell ) or function )

you are in a bash shell which started with no arguments ( /bin/bash )

the output is
Code:
__foo_ barfoo foobar
^^ ^^ ^   ^\  /^
  \/   \     \|___ arguments
   |  spacer 
program
I hope that makes sense
 
Old 09-17-2019, 03:16 PM   #5
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian sid
Posts: 2,320

Rep: Reputation: 635Reputation: 635Reputation: 635Reputation: 635Reputation: 635Reputation: 635
Quote:
Originally Posted by finalturismo View Post
can you break down the meaning of ${@// /+}

I understand the if and, else statements and pipes etc..

its just i don't understand all the weird bash symbols yet. I am trying to graduate from bash grade school here and it is not easy.
sure, it is parameter substitution
https://www.tldp.org/LDP/abs/html/pa...stitution.html

I was using global sub. to replace all the spaces with +

if you don't and your search is more than one word the URL will have spaces in it

+ usually works
 
Old 09-17-2019, 03:21 PM   #6
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian sid
Posts: 2,320

Rep: Reputation: 635Reputation: 635Reputation: 635Reputation: 635Reputation: 635Reputation: 635
doh, actually it wont work

Code:
youtube () {
    search="$@"
    chromium "https://www.youtube.com/results?search_query=${search// /+}"
}
that should
 
Old 09-17-2019, 06:22 PM   #7
tofino_surfer
Member
 
Registered: Aug 2007
Posts: 356

Rep: Reputation: 102Reputation: 102
Quote:
you might need to use %20 instead of + , I haven't checked
On bash this would be \x20 for the space character and \x2B for the '+' character. So it would be

Code:
youtube () {
    search="$@"
    chromium "https://www.youtube.com/results?search_query=${search//\x20/\x2B}"
}
Or in octal

Code:
chromium "https://www.youtube.com/results?search_query=${search//\040/\053}"
 
Old 09-17-2019, 06:58 PM   #8
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian sid
Posts: 2,320

Rep: Reputation: 635Reputation: 635Reputation: 635Reputation: 635Reputation: 635Reputation: 635
turns out it doesn't really matter ( much )

well, at least with Firefox

because I wrapped the url with "" , Firefox automagically sets it %20
then youtube seems to have a little think and redirects to a +ed url

with ${search// /+} you get there quicker


neither ${search//\x20/\x2B} nor ${search//\040/\053} work
they just leave the original space

however, if you were to use sed..
Code:
echo ${search//\x20/\x2B}
<<<${search} sed 's/\x20/\x2B/'
 
Old 09-18-2019, 12:45 AM   #9
tofino_surfer
Member
 
Registered: Aug 2007
Posts: 356

Rep: Reputation: 102Reputation: 102
I wasn't aware of this effect of the quotes. However there is a somewhat convoluted way to use character codes using echo -e in a subprocess.

Code:
$ search="one two three four"
$ echo ${search}
one two three four
$ echo "${search//`echo -e \\\\x20`/`echo -e \\\\x2B`}"
one+two+three+four
$ echo "${search//`echo -e \\\\040`/`echo -e \\\\053`}"
one+two+three+four

$ SPACE=`echo -e \\\\040`
$ PLUS=`echo -e \\\\053`
$ echo "${search//$SPACE/$PLUS}"
one+two+three+four
With sed you need the 'g' at the end to replace all instances of a space.

Code:
$ <<<${search} sed 's/\x20/\x2B/'
one+two three four
$ <<<${search} sed 's/\x20/\x2B/g'
one+two+three+four

Last edited by tofino_surfer; 09-18-2019 at 01:03 AM.
 
Old 09-27-2019, 03:06 PM   #10
finalturismo
Member
 
Registered: May 2019
Posts: 41

Original Poster
Rep: Reputation: Disabled
youtube () {
search="$@"
chromium "https://www.youtube.com/results?search_query=${search// /+}"
}
That worked perfect!


firerat would you be interested in helping me with a hard drive wiper script over a remote application in linux. I can send you a little money for helping me. $100+

Last edited by finalturismo; 09-27-2019 at 03:08 PM.
 
  


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
LXer: Gnome Shell YouTube Search Provider Lets You Play YouTube Videos In VLC LXer Syndicated Linux News 0 07-31-2018 03:32 PM
[SOLVED] help bash script to send request to url and re-writing the result url jmishal Programming 3 04-16-2015 04:46 PM
LXer: Search YouTube videos on Ubuntu with the YouTube Lens LXer Syndicated Linux News 0 12-30-2011 08:40 PM
.bashrc / .bash_profile /etc/profile /etc/bashrc deadeyes Red Hat 2 02-13-2010 11:22 AM
Setting path: /etc/profile, /etc/bashrc or ~/.bashrc Swakoo Linux - General 1 08-07-2007 10:59 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 04:24 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