LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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 12-04-2016, 08:41 PM   #1
L_Carver
Member
 
Registered: Sep 2016
Location: Webster MA USA
Posts: 241

Rep: Reputation: Disabled
BASH: Since when did setting your IFS...


..risk globbing and word-splitting.

My code:
Code:
#!/bin/bash -i
SAVEIFS=$IFS
IFS=$(echo -en "\n\b")

echo "-----------------------------------------------------------------------"
echo "CAPTION AND COMMENT SCRIPT: Uses readline and ExifTool"
echo
echo -e "Files this script uses must be in the following format:"
echo -e "LINE 1: the name of the JPEG to be annotated,"
echo -e "LINE 2: a caption for the JPEG,"
function gettext () {
echo -e "What text file will I be using?"
read -e item
#Allowing for, and correcting, the trailing space in interactive mode
if [[ $item =~ " " ]]; then
	capfile1=${item% *}
else
	capfile1=$item
fi
}
gettext
mapfile -t honest<"$item"
file0=${honest[0]}
cap1=${honest[2]}
echo $file1
echo $cap1
echo "$cap1"
echo "The Caption line looks right. Here goes."
echo "Writing information to $file0"
exiftool -fast5 -overwrite_original_in_place -q -Caption-Abstract="$cap1" -Description="$cap1" $file0
echo "File $file0 Annotated."

IFS="$SAVEIFS"
ShellCheck.com's analysis included the lines
SC2086: Double quote to prevent globbing and word splitting.
I thought setting your IFS in the second line of a script removed the need to double-quote anything. Is it the ugly "/b" in my IFS declaration that spoils it?

Someone please explain.

Carver
 
Old 12-05-2016, 01:12 AM   #2
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 1,766

Rep: Reputation: 797Reputation: 797Reputation: 797Reputation: 797Reputation: 797Reputation: 797Reputation: 797
I have no experience with setting IFS globally - one would really have to learn the effects.

But I have good experiences with
Code:
set -f
that prevents globbing. An "eval" becomes less risky; so is a forgotten quoting.
Nevertheless one should always quote variables in command arguments to prevent field splitting.
 
Old 12-05-2016, 01:42 AM   #3
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 16,611

Rep: Reputation: 5606Reputation: 5606Reputation: 5606Reputation: 5606Reputation: 5606Reputation: 5606Reputation: 5606Reputation: 5606Reputation: 5606Reputation: 5606Reputation: 5606
Quote:
I thought setting your IFS in the second line of a script removed the need to double-quote anything. Is it the ugly "/b" in my IFS declaration that spoils it?
I'm not really sure if shellcheck can handle this case (at all) properly, you need to check the documentation.
I think: setting IFS and using " to protect your string are two different things therefore the SC2086 is still valid (your string may contain \n too).

You do not need to restore IFS at the end of the script, this setting is valid only inside the script, will be forgotten when it is completed.
 
Old 12-05-2016, 02:08 AM   #4
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=14, FreeBSD_12{.0|.1}
Posts: 5,650
Blog Entries: 11

Rep: Reputation: 3686Reputation: 3686Reputation: 3686Reputation: 3686Reputation: 3686Reputation: 3686Reputation: 3686Reputation: 3686Reputation: 3686Reputation: 3686Reputation: 3686
Quote:
Originally Posted by L_Carver View Post
I thought setting your IFS in the second line of a script removed the need to double-quote anything.
No, not as stated anyway. I have never heard that statement before and would be interested to know the source or context.

Setting the IFS at the start of your script blocks certain attack vectors which exploit a maliciously crafted IFS in the environment. Most current shells do not inherit IFS and set it explicitly so that is not generally a concern these days.

And of course, if your script requires a particular IFS then it should be set.

But to quote, or not to quote depends on the immediate context in every case, and is not wholly determined by IFS as the above statement seems to suggest.

Last edited by astrogeek; 12-05-2016 at 02:53 AM. Reason: Added 'to quote or not...'
 
  


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
BASH:Rewrite a function using parameter expansion instead of changing IFS Mes9 Programming 7 09-26-2012 02:50 PM
Bash: when an empty IFS does not work like a default IFS (info) catkin Programming 13 04-19-2012 09:40 AM
Resetting Internal Variables to Default (BASH, IFS in particular) rm_-rf_windows Linux - General 4 03-08-2012 05:26 PM
setting IFS to ignore pattern eRJe Programming 3 03-12-2010 04:20 AM
setting IFS variable infamous41md Linux - Newbie 2 05-20-2003 06:12 PM

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

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