ghantoos / lshell

lshell is a shell coded in Python, that lets you restrict a user's environment to limited sets of commands, choose to enable/disable any command over SSH (e.g. SCP, SFTP, rsync, etc.), log user's commands, implement timing restriction, and more.
GNU General Public License v3.0
435 stars 112 forks source link

exit codes from shell commands #119

Closed lmarkov closed 8 years ago

lmarkov commented 8 years ago

Hello, Can you tell me how work the exit codes from shell commands? I saw that there is some kind of rewriting but may be I have mistake.

 :~# alksjdas
 -bash: alksjdas: command not found
~# echo $?
127
cd alksjdas
-bash: cd: alksjdas: No such file or directory
:~# echo $?
1
:~# nano test.txt
:~# echo $?
0

I want to filter the real shell exit codes but in python I get only 0 or 1 as a result.

ghantoos commented 8 years ago

I based my exit codes on some tests, but it looks like they are not the same for all commands.

Example:

~$ ls qweqwe
ls: cannot access qweqwe: No such file or directory
~$ echo $?
2

and

~$ cd qweqwe
-bash: cd: qweqwe: No such file or directory
~$ echo $?
1

I will correct the cd exit code, as well as the "command not found".

Thanks for reporting this.

lmarkov commented 8 years ago

Yes, there are different codes. For example: http://tldp.org/LDP/abs/html/exitcodes.html

Can you suggest how can I fix it and make exception for example when I type

# blabla
-bash: blabla: command not found
# echo $?
127

to except this error. I use additional script which creates tickets when some command or path is forbidden but I don't want to receive information for such errors every time. It could be useful this codes to be more flexible and may be to be included in the configuration files.

ghantoos commented 8 years ago

There is a inconsistency between bash(1) and sh(1) on the cd built-in exit code.

On sh(1):

$ cd qweqwe
sh: 5: cd: can't cd to qweqwe
$ echo $?
2

On bash(1):

~$ cd qweqwe
-bash: cd: qweqwe: No such file or directory
~$ echo $?
1

The bash documentation seems to point to an exit code of 2: http://tldp.org/LDP/abs/html/exitcodes.html

So I decided to keep the exit code provided by the python os.chdir function, for consistency.

Otherwise, I will be correcting the other exit code in my next commit.