1753943 Members
8876 Online
108811 Solutions
New Discussion юеВ

Re: command problem

 
neocosmic
Occasional Contributor

command problem

1. How to excute script without creating new subshell?

2. How to prevent a file from being removed?
4 REPLIES 4
Francisco J. Soler
Honored Contributor

Re: command problem

1.- If you are in bash shell with the dot command, but this is not a properly execution, is like if you type in the commands in the shell.

$ . script.sh

(dot space bar script name)

2.- You can do it in two ways
a) chmod 444 file_name
but with rm -f file_name, if you are the owner the file is removed anyway.
b) chmod 444 file_name and changing the owner of the file,
chown root.root file_name. But, at this way, you can't access the file anymore.

Frank.
Linux?. Yes, of course.
Bill Douglass
Esteemed Contributor

Re: command problem

You can use the exec command to replace your current shell with the specified command, instead of spawning a sub-shell to run the command. This will cause your session to exit once the command completes.

If you're talking about shell scripts only, your can source the script file with the . command, e.g.

. /path/to/script

Keep in mind that, if your shell script executes an exit command, your session will be terminated, just as if you had used the exec command. In this case, replace exit with return.
Stuart Browne
Honored Contributor

Re: command problem

As the 1st question has been answered, I won't re-hash.

The 2nd answer however, there are a number of solutions, depending on how much you want the file to stay.

The method Francisco should be fine if you don't want to protect the file from it's owner.

If you want further protection, you can use 'chattr'.

One of the flags you can give files are 'i' (immutable).

This might be more protection than you want however. From the man page of 'chattr':

A file with the `i' attribute cannot be modified: it cannot be deleted or renamed, no link can be created to this file and no data can be written to the file. Only the superuser can set or clear this attribute.

.. Just another option for you.
One long-haired git at your service...

Re: command problem

1. either
# . ./script_name
or
# exec ./script_name

2. chattr +i file

Now even root cannot delete file. Unless he also runs the command on that file and then tries rm again

Regards,
Trystan