Linux, FreeBSD, Juniper, Cisco / Network security articles and troubleshooting guides

FAQ
It is currently Wed Oct 04, 2023 9:29 pm


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

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

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

Offline
 

Linux Shell: Protecting against accidental output redirection and file overwrite

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

_________________
Humble user
http://www.ivorde.ro





Top
Display posts from previous:  Sort by  
E-mail friendPrint 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

6584

Mon Aug 24, 2009 10:26 am

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

4703

Thu Jan 14, 2010 5:58 pm

debuser View the latest post

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

debuser

0

5574

Thu Nov 26, 2009 4:49 am

debuser View the latest post

There are no new unread posts for this topic. How to change crontab editor in Debian Linux

mandrei99

1

7117

Tue Mar 08, 2016 6:53 pm

tsuchan View the latest post

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

debuser

0

4258

Fri Apr 09, 2010 7:52 am

debuser View the latest post

 

Who is online
Users browsing this forum: No registered users and 0 guests
You can post new topics in this forum
You can 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:  
cronNews 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 - 5 hours [ DST ]



phpBB SEO