TAB completion and mc problems - bash

DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=8.10
DISTRIB_CODENAME=intrepid
DISTRIB_DESCRIPTION="Ubuntu 8.10"
This is the server version.
When I ssh into it, I encounter the following problems:
Problem 1
tab completion behaves weird to the point of being unusable:
> cd ~/<press TAB>
-sh: <( compgen -d -- '/home/dmitriid/' ): No such file or directory
> vi ~/.<press TAB>
<( compgen -d -- '/home/dmitriid/.' ): No such file or directory
-sh: <( eval compgen -f -X '*.#(o|so|so.!(conf)|a|rpm|gif|GIF|jp?(e)g|
JP?(E)G|mp3|MP3|mp?(e)g|MPG|avi|AVI|asf|ASF|ogg|OGG|class|CLASS)' --
$(quote_readline $cur) ): No such file or directory
> nano ~/.<press TAB>
./ .bash_logout .mc/ .viminfo
../ .bashrc .mysql_history
.aptitude/ .erlang.cookie .profile
.bash_history .gitconfig .ssh/
Is there a way to fix that?
Problem 2
I use mc quite a lot. I often do a Ctrl+O to hide panels and work in the shell. In my case:
Ctrl + O hides panels
Any keypress brings the panels back
Is there a way to fix that as well?
Thank you!

Ok. It turns out the solution was dumb and simple. The key was — no interactive shell in mc.
The answer lies here: http://www.ibiblio.org/mc/FAQ
6.6 When I use Ctrl-O I don't get a subshell. How do I fix this?
Only bash, tcsh and zsh can be used as subshell. Use one of those
shells as your default shell, and it will be used as subshell in GNU
Midnight Commander.
So I decided to change the shell:
> which bash
/bin/bash
> sudo chsh -s /bin/bash my_user_name
> grep ^my_user_name /etc/password
my_user_name:x:1002:1002::/home/my_user_name:/bin/bash
Note /bin/bash at the end of the passwd file. It means the shell is now changed.
After i logged out and then logged back in — voila, everything works!

I'd try to reinstall all bash packages, especially bash-completion: apt-get install --reinstall bash-completion, because it looks like some part of bash is screwed. Btw, I guess this should go to serverfault.com.

When I typed the following to switch to root, then it seemed that the permissions problem was resolved. Looks like compgen isn't able to read the directories it needs to when I'm logged in as an regular user.
sudo su -

"6.6 When I use Ctrl-O I don't get a subshell. How do I fix this?"
To use Ctr+o you should have SHELL=/bin/bash and not SHELL=/bin/sh
Add to .bashrc export SHELL=/bin/bash

Related

Sending Bash Aliases to detached screen sessions

I'm on a Linux machine using screen, and I'm attempting to write a (fairly portable) function which runs a bash function in a new, detached screen session which automatically closes upon completion. I've had some success, but I noticed the following behavior:
If I include the definition of mail_submit() in my ~/.bashrc file, I can run
mail_submit foo
in the terminal, and also I can access the alias in a new screen session:
screen -S test
mail_submit foo
However, the following command does not work:
screen -d -m -S test sh -c 'mail_submit foo'
presumably because sh -c starts a fresh shell that has no knowledge of my ~/.bashrc profile. So, I can use the following fix:
screen -d -m -S test sh -c 'source ~/.bashrc; mail_submit foo'
which does work.
But if I want to wrap this functionality up into a bash alias (which is my ultimate goal here), this will cause a weird self-referential situation.
Question: What is an easy way to either have sh -c know the location of my ~/.bashrc profile, or use a variant of sourcing the file and creating an alias?
EDIT: I could save the shell script in my home directory, and create an alias which runs
screen -d -m -S test bash -c '~/mail_submit.sh $1'
but I'd still be curious to hear other possible fixes.
A default ~/.bashrc contains this ([[ "$-" != *i* ]] && return) little piece of code on top of it (or somewhere else in the upper part). This line will prevent the ~/.bashrc from beeing sourced if the bash shell doesn't run in interactive mode.
You could:
Remove this line
Create a new file which will only contain the alias you need and source that
Create a little bash script instead of an alias and run that
Do you mean screen -d -m -S test bash -c 'mail_submit foo'?
It looks like you're trying to run the command with the shell (sh), and not the bourne again shell (bash), which is the shell interpreter which actually reads the ~/.bashrc profile.
Edit: The .bashrc file is not being sourced by default because screen does not create the bash process as a login shell, which is when the .bashrc file is read. Creating a .screenrc file with the line defshell -bash will create the bash process as a login shell instead, which will then call the .bashrc file.

I changed /etc/passwd to git-shell and changed it back, but now shell not working

I was following the instruction on how to set up a git server here, and I removed the ssh access from the git user by setting /etc/passwd file to /usr/bin/git-shell.
Later I found out that I still want to ssh as git, so I reset it back to /bin/sh. I could ssh back, but the shell prompt looked all strange today. Previously, the prompt was
git#xyz.com $
but now it's just
$
Tab Autocomplete is gone. History is also gone. I am not sure what files got removed when I switched to git-shell. How do I recover from this?
chsh git -s /bin/bash should fix it (I prefer to use the chsh program instead of editing /etc/passwd by hand to change an account's login shell)
Maybe it was not /bin/sh before, try setting it as
/bin/bash

tmux: open terminal failed: missing or unsuitable terminal: xterm-256color

I used home-brew to setup tmux on a mac. When trying to run tmux I keep on getting this error
open terminal failed: missing or unsuitable terminal: xterm-256color
any suggestions?
Your system doesn't have xterm-256color. You could:
Set TERM to something other than xterm-256color outside tmux (try just plain export TERM=xterm).
See if there is a package containing xterm-256color, perhaps a later version of ncurses or terminfo.
Install it manually from another system with something like:
infocmp -x xterm-256color > out
Then transfer the "out" file to your Mac and try:
tic out
This happened to me during a system upgrade. Unfortunately I did not see a way besides restarting: tmux kill-server and then run tmux.
you can just type export TERM=xterm in console when you see this error, or put export TERM=xterm in the file ~/.bash_profile and source ~/.bash_profile. then you may never get this error again.
This works fine on my debian.
TL;DL
sudo ln -sf /usr/share/terminfo/x/xterm-color /usr/share/terminfo/x/xterm-256color
Details
Maybe the file /usr/share/terminfo/x/xterm-256color is corrupted. It may be fixed upon (system/package) upgrade / reinstall.
Meanwhile you can use other terminfo entry. You can get the available options by ls /usr/share/terminfo/x.
I am having the same problem and using xterm-16color meanwhile.
To set terminfo, you can type reset in the terminal, then select the terminfo:
username:~$ cd /usr/share/terminfo/x
username:x$ ls
x10term xnuppc+200x64 xterm-24 xterm+pcfkeys
x1700 xnuppc-200x64-m xterm-256color xterm-pcolor
x1700-lm xnuppc-200x75 xterm+256color xterm-r5
...
username:x$ reset
reset: unknown terminal type xterm-256color
Terminal type? xterm-16color
=== Edit 2018 Feb 20 ===
You should config the term in many places if you want to make it permanent, for example, .vimrc, .tmux.config, .Xresources, e.t.c.
I still have issue when using terminator -x 'tmux attach -t music; exec bash'
So I'm using a quick hack as below:
cd /usr/share/terminfo/x
sudo mv xterm-256color xterm-256color.bk
sudo ln -sf /usr/share/terminfo/x/xterm-color /usr/share/terminfo/x/xterm-256color
When I removed some sessions/windows this error went away for me. Apparently I had too many TTY's open.
You can kill sessions/windows from outside tmux with these commands:
tmux kill-session -t <session-name>
tmux kill-window -t <session-name>:<window-name>
I am also having the same issue.
export TERM=xterm
tmux kill-server
This helps solve the problem: https://www.peterdavehello.org/2019/11/tmux-open-terminal-failed-missing-or-unsuitable-terminal-xterm-256color/
In my case Xterm was absent.
(But I had the same error on Ubuntu)

Applying sudo to some commands in script

I have a bash script that partially needs to be running with default user rights, but there are some parts that involve using sudo (like copying stuff into system folders) I could just run the script with sudo ./script.sh, but that messes up all file access rights, if it involves creating or modifying files in the script.
So, how can I run script using sudo for some commands? Is it possible to ask for sudo password in the beginning (when the script just starts) but still run some lines of the script as a current user?
You could add this to the top of your script:
while ! echo "$PW" | sudo -S -v > /dev/null 2>&1; do
read -s -p "password: " PW
echo
done
That ensures the sudo credentials are cached for 5 minutes. Then you could run the commands that need sudo, and just those, with sudo in front.
Edit: Incorporating mklement0's suggestion from the comments, you can shorten this to:
sudo -v || exit
The original version, which I adapted from a Python snippet I have, might be useful if you want more control over the prompt or the retry logic/limit, but this shorter one is probably what works well for most cases.
Each line of your script is a command line. So, for the lines you want, you can simply put sudo in front of those lines of your script. For example:
#!/bin/sh
ls *.h
sudo cp *.h /usr/include/
echo "done" >>log
Obviously I'm just making stuff up. But, this shows that you can use sudo selectively as part of your script.
Just like using sudo interactively, you will be prompted for your user password if you haven't done so recently.

Switching from zsh to bash on OS X, and back again?

I'm learning to develop in Rails, and have discovered the power of zsh. However, for some of my other tasks, I wish to use normal bash.
Although they are the same, I just feel comfortable with the layout of bash in some situations.
How do I switch back and forth, or turn zsh on and off?
You can just use exec to replace your current shell with a new shell:
Switch to bash:
exec bash
Switch to zsh:
exec zsh
This won't affect new terminal windows or anything, but it's convenient.
you can try chsh -s /bin/bash to set the bash as the default,
or chsh -s /bin/zsh to set the zsh as the default.
Terminal will need a restart to take effect.
I switch between zsh and bash somewhat frequently. For a while, I used to have to source my bash_profile every switch. Then I found out you can (typically) do
exec bash --login
or just
exec bash -l
if it is just a temporary switch
you can use exec as mentioned above, but for more of a permanent solution.
you can use chsh -s /bin/bash (to switch to bash) and chsh -s /bin/zsh (to switch to zsh)
For Bash, try
chsh -s $(which bash)
For zsh, try
chsh -s $(which zsh)
In Mac OS Catalina default interactive shell is zsh.
To change shell to zsh from bash:
chsh -s /bin/zsh
Then you need to enter your Mac password. Quit the terminal and reopen it. To check whether it's changed successfully to ssh, issue the following command.
echo $SHELL
If the result is /bin/zsh, your task is completed.
To change it back to bash, issue the following command on terminal.
chsh -s /bin/bash
Verify it again using echo $SHELL. Then result should be /bin/bash.
zsh has a builtin command emulate which can emulate different shells by setting the appropriate options, although csh will never be fully emulated.
emulate bash
perform commands
emulate -R zsh
The -R flag restores all the options to their default values for that shell.
See: zsh manual
you can just type bash or if you always want to use bash:
on "iTerm2"
Go to preferences > Profiles > Command
Select "Command" from the dropdown
Type bash
Test by closing iTerm and open it again
You should be able just to type bash into the terminal to switch to bash, and then type zsh to switch to zsh. Works for me at least.
Follow the below steps !
chsh -s /bin/bash
Restart terminal
check which shell is in use by echo $SHELL
source .profile
You are back with Bash !!
For me, the solution was this:
Edit:
sudo vi /etc/passwd
Find your user, for me it was for example:
ubuntu:x:1000:1001::/home/ubuntu:/bin/sh
For you it might be:
ubuntu:x:1000:1001::/home/ubuntu:/bin/zsh
And change it to:
ubuntu:x:1000:1001::/home/ubuntu:/bin/bash
If you want bash to be defaul, or the line above if you want it to be zsh by default.
You can easily switch back to bash by using command "bye"

Resources