Ivorde Unix/Linux/Database/Web/Mail Forum

View unanswered posts
View active topics
It is currently Wed Feb 08, 2012 8:29 am


Guides and tutorials for Red Hat Linux, Debian, Suse and others.

Bookmark this article:

Author Message
LaR3
  Post  Post subject: Linux Shell: Protecting against accidental output redirection and file overwrite  |  Posted: Mon Aug 24, 2009 5:14 pm

Joined: Mon Aug 03, 2009 11:55 pm
Posts: 19

Offline
Save on Delicious
Redirecting standard output/error in a Linux/BSD shell can very easy overwrite a file, removing it's content, making it impossible to recover (unless there's a backup).

To prevent this sh, bash and ksh shells implement an evironment variable called noclobber.

Code:
# cat tmp.file
If you use a lot of file redirection in your scripts it's quite easy to overwrite existing files accidentaly.
To prevent this, most shells implement the noclobber environment variable.

# echo "Overwritten content">tmp.file
# cat tmp.file
Overwritten content


Checking if noclobber is activated in current shell:
Code:
# set -o
allexport       off
braceexpand     on
emacs           on
errexit         off
errtrace        off
functrace       off
hashall         on
histexpand      on
history         on
ignoreeof       off
interactive-comments    on
keyword         off
monitor         on
noclobber       off
noexec          off
noglob          off
nolog           off
notify          off
nounset         off
onecmd          off
physical        off
pipefail        off
posix           off
privileged      off
verbose         off
vi              off
xtrace          off


Activating shell noclobber environment variable:
Code:
# set -o noclobber; set -o | grep noclobber
noclobber       on


At this point, accidental output redirections cannot harm. An error will occur when this will happen:
Code:
# echo "2nd Overwritten content">tmp.file
bash: tmp.file: cannot overwrite existing file


Top
Display posts from previous:  Sort by  
Print view

Topics related to - "Linux Shell: Protecting against accidental output redirection and file overwrite"
 Topics   Author   Replies   Views   Last post 
There are no new unread posts for this topic. Linux Shell: "Cannot overwrite existing file" when the file is owned by you

LaR3

0

1798

Mon Aug 24, 2009 5:26 pm

LaR3 View the latest post

There are no new unread posts for this topic. Linux change the password with output from echo command (standard input)

debuser

0

1481

Fri Jan 15, 2010 12:58 am

debuser View the latest post

There are no new unread posts for this topic. Linux reloading inittab without reboot

debuser

0

1676

Thu Nov 26, 2009 11:49 am

debuser View the latest post

There are no new unread posts for this topic. Linux umount -f: umount2: Device or resource busy

debuser

0

1991

Fri Apr 09, 2010 2:52 pm

debuser View the latest post

 

Who is online
Users browsing this forum: No registered users and 0 guests
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum
Jump to:  
News News Site map Site map SitemapIndex SitemapIndex RSS Feed RSS Feed Channel list Channel list


Delete all board cookies | The team | All times are UTC + 2 hours [ DST ]

Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
DAJ Glass 2 template created by Dustin Baccetti

phpBB SEO