'cd' ing inside a shell script
Bill Campbell
freebsd at celestial.com
Tue Nov 7 18:42:46 UTC 2006
On Tue, Nov 07, 2006, Grant Peel wrote:
>Hi all,
>Some of my webmail users are using lots of disk space.
>I want to make a shell script, like so:
>cd /home/webmail/public_html/cgi-bin/etc/users ; du -h -d1 | grep M | sort
>-nr -k1
>and run it daily in a cronjob, mailing the results to my assistants.
>with the 'cd' at that start of the script have any adverse affect on the
>rest of the system or cronjobs? OK, will the scripot just complete and
>exit, with the 'cd' having no affect?
A cd inside a script will have no effect on anything outside of
the script. Only brain-dead systems from Microsoft do that.
In fact, within a script, there's a safe way to do cd's that
doesn't affect the directory which can be very handy when writing
scripts that may not want to keep track of where they are:
#!/bin/sh
# do something here
(
cd somedirecotry
# you're now in somedirectory to do something
)
# now you're back in the original directlry.
A new shell is spawned for the process inside the parenthesis,
and any directory changes etc. will have no effect outside the
parenthesis.
Bill
--
INTERNET: bill at Celestial.COM Bill Campbell; Celestial Software LLC
URL: http://www.celestial.com/ PO Box 820; 6641 E. Mercer Way
FAX: (206) 232-9186 Mercer Island, WA 98040-0820; (206) 236-1676
When a place gets crowded enough to require ID's, social collapse is
not far away. It is time to go elsewhere. The best thing about space
travel is that it made it possible to go elsewhere.
-- Robert Heinlein
More information about the freebsd-questions
mailing list