1 What happened?
Today when I tried to less a file, it printed nothing and caused the terminal a mess.
$ cat t.txt
HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH
$ cat t.txt | less # here the less works normally
HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH
(END)
$ less t.txt # nothing output here!!!
$ # the shell is dead now
2 Why?
$ cat /etc/passwd | grep user01
user01:x:1001:1001::/home/user01:/bin/csh
$ cat ~/.cshrc
/bin/bash -l
The account "user01" has /bin/csh as its login shell. But it wants to use /bin/bash instead, so it invokes a bash automatically by taking advantage of .cshrc.
As a result, every time a "csh" is executed, whether it's login-shell or not, the .cshrc would be run automatically.
The "less t.txt" command uses $SHELL to run some work under the hood. Here $SHELL is /bin/csh which for sure will load .cshrc.
The .cshrc intercepts the csh script and run a login bash instead.
less t.txt --> /bin/csh --> ~/.cshrc --> /bin/bash -l
3 Solution
Only login csh should invoke a login bash.
$ cat ~/.cshrc
if (x$0 == 'x-csh') then
/bin/bash -l
endif
4 Conclusion
Run an infinite loop in a script is not good. Invoking an interactive shell is one kind of infinite loop.Actually, staring another interactive shell in an existing shell's RC file can cause lots of surprises. Another common problem caused by the same reason can be found at https://www.linuxexam.net/2020/03/ansible-is-stuck-ssh-command-is-stuck.html.
No comments:
Post a Comment