LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
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 08-15-2010, 06:14 PM   #1
haleba
LQ Newbie
 
Registered: Aug 2010
Posts: 2

Rep: Reputation: 0
Help Requested with bash Parameter Substitution


I just finished reading a fascinating thread about a parameter substitution problem in bash titled "Yet Another Bash Quotes Within Quotes Issue".

I'm new to learning Unix/Linux (and this group) and am working on a simple script. Filing this thread under "Obscure Scripting Features that May Bite You on the Ass" I read through for future reference.

The problem involved getting a script to generate an e-mail message titled "Error from daily_file_backup" to root. This person wanted to use:

MAIL="/bin/mailx -s Error from $(basename $0) root"
/bin/umount /data_backup||{ echo "backup unmount failed"|$MAIL;exit 1;

with quotes around "Error from $(basename $0)" so that $0 would expand into daily_file_backup.

After a lot of back and forth, the solution was to use:

basename ${0/-//}

or "basename ${0/-//}", it isn't clear which.

Then the thread died without an explanation why this would work!!!!!!!!!!!!!!!!!!!!!!!!!

The user was happy, but WTF? I've tried all kinds of bash tutorials, but this type of construction doesn't appear, except that "/" and "//" are used for substring replacement. That's as far as I get.

Rather that posting this to the original thread--which appears to have died quite a few months ago--I'm posting this new thread.
 
Old 08-15-2010, 06:29 PM   #2
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Without the full context of the original, specifically the exact name of the script as contained in $0, it's hard to know exactly what it's supposed to do. But you're right in that it is the substring replacement. Specifically, it replaces the first instance of "-" with "/". Then basename strips the rest of the filepath off to give you just the name of the script itself.

Note also that quotation marks inside subshell constructions like $() are treated as separate from the ones in the rest of the command.
 
Old 08-15-2010, 07:38 PM   #3
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Rep: Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043
Run your script with sh -x yourscript

It will become obvious what is interpreted in the wrong way. Then you can start working on a solution.

jlinkels
 
Old 08-23-2010, 01:37 PM   #4
haleba
LQ Newbie
 
Registered: Aug 2010
Posts: 2

Original Poster
Rep: Reputation: 0
Just got an e-mail reminder on the thread (blush). I appreciate the quick assistance, but I haven't had time to follow through on the suggestions.
 
  


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] bash variable substitution Jerry Mcguire Programming 6 04-29-2010 09:33 AM
Parameter passing: Parameters in General Info requested theKbStockpiler Programming 9 04-27-2010 08:52 PM
Bash Command Substitution dakensta Programming 5 11-30-2006 03:10 PM
linux bash - how to use a dynamic parameter in shell parameter expansion expression nickleus Linux - General 2 08-21-2006 04:54 AM
Bash variable substitution daYz Programming 3 04-14-2006 01:16 PM

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

All times are GMT -5. The time now is 01:54 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
Open Source Consulting | Domain Registration