LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-13-2020, 12:36 PM   #1
ziphem
Member
 
Registered: Feb 2004
Location: US / EU
Distribution: Fedora 20
Posts: 183

Rep: Reputation: 19
Bash scripting: how to handle ampersand in a string for a positional parameter?


Hello all!

I'm really stumped on this. I've been having fun in my .bashrc with shell functions, including making one for a command "pull." But I'm having a hard time figuring out how to handle strings that include "&". For instance:

~]$ pull file domain.tr/1em121cww

works no problem.

But

~]$ pull file domain.tr/1em121cww&s3fs12kfs

does not.

I know the easy solution is on the command line to simply write

~]$ pull file "domain.tr/1em121cww&s3fs12kfs"

but it's not so easy when you have multiple filenames (making use of positional parameter "${@:2}").

Is there some other way of handling the ampersand in the shell function? I thought maybe with sed, but this ampersand creates an issue before the shell function runs. I also don't want to process (escape, quote) every bash input with ampersands - just what I use for the "pull" command.

Thanks a ton for any help!
 
Old 01-13-2020, 03:00 PM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 3,942

Rep: Reputation: 1417Reputation: 1417Reputation: 1417Reputation: 1417Reputation: 1417Reputation: 1417Reputation: 1417Reputation: 1417Reputation: 1417Reputation: 1417
Could you please explain the problem with multiple filenames?
 
Old 01-13-2020, 04:17 PM   #3
ziphem
Member
 
Registered: Feb 2004
Location: US / EU
Distribution: Fedora 20
Posts: 183

Original Poster
Rep: Reputation: 19
Hi, thanks for the reply.

It's really a control operator issue. The issue I have is when I try to run the command on what is positional parameter and there is a character that is also a control operator - sorry if I wasn't clear on this in my first post. For instance:

~]$ dl song domain.tr/1em121cwws3fs12kfs

does work.

But

~]$ dl song domain.tr/1em121cww&s3fs12kfs

does not work.

In my .bashrc, I have this as part of a number of other lines:

Code:
dl() {

# First, we define the variables:
TorLaunch="/home/me/location1/tor-browser_en-US/Browser/start-tor-browser"
YtDL_base="youtube-dl --no-progress --console-title --ignore-errors --no-mtime"
YtDL_audio="-x --audio-format mp3 -f bestvideo[ext=mp4]+bestaudio[ext=m4a]/bestvideo+bestaudio -k --embed-subs"

    if [[ $1 == "song" ]]; then
while true; do
    read -p "(T)or download, (D)ownload, or (Q)ueue? : " tdq
    case $tdq in

        [Tt]* )
$TorLaunch &
until [ "$(proxychains4 curl -v --silent icanhazip.com  2>&1 | grep -i fail | wc -l)" -lt 1 ] || (( count++ >= 5 )) ;
do echo "Attempting to connect Tor..." && sleep 3;
done
proxychains4 -q $YtDL_base -o /home/me/location2/youtube-dl/'%(title)s'.flv $YtDL_audio "${@:2:99}"
break;;

        [Dd]* )

$YtDL_base -o /home/me/location2/youtube-dl/'%(title)s'.flv $YtDL_audio "${@:2:99}"
break;;

...and so on and so forth...

Last edited by ziphem; 01-14-2020 at 02:37 AM.
 
Old 01-13-2020, 04:43 PM   #4
boughtonp
Member
 
Registered: Feb 2007
Location: UK
Distribution: Debian
Posts: 119

Rep: Reputation: 62
That issue occurs before the "dl" script code runs - ampersand is a special character, you need to escape it or wrap the input in single-quotes to have Bash treat it as a literal.
Quote:
dl song 'domain.tr/1em121cww&s3fs12kfs'


(Unrelated to your issue, but instead of "${@:2:99}" you may prefer adding shift 1 after you've extracted the first argument, so you can then just use "$@" to get the rest.)


Last edited by boughtonp; 01-13-2020 at 04:47 PM.
 
1 members found this post helpful.
Old 01-13-2020, 10:08 PM   #5
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 9,316

Rep: Reputation: 4116Reputation: 4116Reputation: 4116Reputation: 4116Reputation: 4116Reputation: 4116Reputation: 4116Reputation: 4116Reputation: 4116Reputation: 4116Reputation: 4116
You need to quote the URL or escape the ampersand. Sorry.

Last edited by dugan; 01-14-2020 at 10:06 AM.
 
Old 01-13-2020, 10:50 PM   #6
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 3,942

Rep: Reputation: 1417Reputation: 1417Reputation: 1417Reputation: 1417Reputation: 1417Reputation: 1417Reputation: 1417Reputation: 1417Reputation: 1417Reputation: 1417
@OP Please use [code] tags not [quote] tags when inserting code.

You could only solve this if you used a special shell that doesn't support background jobs (so &ampersand isn't special character) -- I don't know of such shell though.

Last edited by NevemTeve; 01-14-2020 at 12:03 AM.
 
Old 01-14-2020, 01:45 AM   #7
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,760

Rep: Reputation: 3050Reputation: 3050Reputation: 3050Reputation: 3050Reputation: 3050Reputation: 3050Reputation: 3050Reputation: 3050Reputation: 3050Reputation: 3050Reputation: 3050
Further to this suggestion:
Quote:
(Unrelated to your issue, but instead of "${@:2:99}" you may prefer adding shift 1 after you've extracted the first argument, so you can then just use "$@" to get the rest.)
Assuming you wanted all but the first, obviously above is best suggestion, but the 99 is superfluous unless you are imagining 100s of parameters and you are limiting to less than 100??
 
Old 01-14-2020, 03:18 PM   #8
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 13,147
Blog Entries: 9

Rep: Reputation: 3601Reputation: 3601Reputation: 3601Reputation: 3601Reputation: 3601Reputation: 3601Reputation: 3601Reputation: 3601Reputation: 3601Reputation: 3601Reputation: 3601
Strange. "$@" should Do The Right Thing wrt double quotes - so I guess "${@:2:99}" should also do it.
- try the shift mentioned, then just "$@"
- try "$*" as well
and show us what it does.

edit:
bash version?
distro?

Last edited by ondoho; 01-14-2020 at 03:20 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
[SOLVED] interpret bash positional parameter within awk regex string vincix Programming 15 03-12-2018 05:33 AM
Positional Parameter issue in Bash script torrid Programming 7 01-31-2017 05:41 AM
Shifting positional parameter inside a function rylphs Linux - Newbie 9 10-19-2010 02:03 PM
bash script ampersand rewrites string - why? misha680 Programming 1 01-09-2010 02:25 PM
trouble with positional parameter $0 on bash. linux distro: ubuntu breezy linrookie Linux From Scratch 2 05-07-2006 01:22 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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