LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
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, 11:36 AM   #1
ziphem
Member
 
Registered: Feb 2004
Location: US / EU
Distribution: Fedora 31
Posts: 225

Rep: Reputation: 29
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, 02:00 PM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

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

Original Poster
Rep: Reputation: 29
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 01:37 AM.
 
Old 01-13-2020, 03:43 PM   #4
boughtonp
Senior Member
 
Registered: Feb 2007
Location: UK
Distribution: Debian
Posts: 3,599

Rep: Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546
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 03:47 PM.
 
1 members found this post helpful.
Old 01-13-2020, 09:08 PM   #5
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,222

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

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

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
@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-13-2020 at 11:03 PM.
 
Old 01-14-2020, 12:45 AM   #7
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,006

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
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, 02:18 PM   #8
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053
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 02:20 PM.
 
Old 01-21-2020, 04:53 PM   #9
ziphem
Member
 
Registered: Feb 2004
Location: US / EU
Distribution: Fedora 31
Posts: 225

Original Poster
Rep: Reputation: 29
Ok, thanks a lot for this feedback! I'll give those a try. I was avoiding shift because it destroys the positional parameter, and I thought I'd preserve them because I'm a saving kind of guy. I'll give it a shot and report back.

Bash version: 5.0.11(1), Fedora 30.
 
Old 01-21-2020, 07:50 PM   #10
Geist
Member
 
Registered: Jul 2013
Distribution: Slackware 14 / current
Posts: 442

Rep: Reputation: 196Reputation: 196
If you're dealing with web stuff like that then you might want to put an en/decoder for URL stuff there anyway.
In case you want an upload function at one point, or something, too.
 
  


Reply



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 04:33 AM
Positional Parameter issue in Bash script torrid Programming 7 01-31-2017 04:41 AM
Shifting positional parameter inside a function rylphs Linux - Newbie 9 10-19-2010 01:03 PM
bash script ampersand rewrites string - why? misha680 Programming 1 01-09-2010 01:25 PM
trouble with positional parameter $0 on bash. linux distro: ubuntu breezy linrookie Linux From Scratch 2 05-07-2006 12:22 PM

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

All times are GMT -5. The time now is 05:50 PM.

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
Open Source Consulting | Domain Registration