Showing posts with label shell. Show all posts
Showing posts with label shell. Show all posts

Thursday, February 12, 2015

Accessing other user's screen session

This need usually arises, when in a multi user machine, you as an admin wanted to check what is other user is running using screen. The best you can see even as root using ps, is just the name of the command, like below:

michael@vbox:~$ sudo ps awxuf | grep -i screen

root      1135  0.0  0.0  13636   976 pts/1    S+   13:27   0:00                          \_ grep --color=auto -i screen

john   4245  0.0  0.0 387364 16668 ?        Sl   Feb02   0:06  |       \_ gnome-screensaver

1001      6762  0.0  0.0 347384 10428 ?        Sl   Feb02   0:00              \_ gnome-screensaver

john    625  0.0  0.0  31320  1568 ?        Ss   11:57   0:00 SCREEN -S test

michael@vbox:~$ pstree -Gap 625

screen,625 -S test

  └─bash,626



When you try to access the screen session using other user, this is usually the error:

michael@vbox:~$ sudo -u john screen -r 625
Cannot open your terminal '/dev/pts/1' - please check.


This is because, your terminal: /dev/pts/1 is only readable and writable to the owner of the terminal:


michael@vbox:~$ ls -lh /dev/pts/1
crw--w---- 1 john tty 136, 1 Feb  12 13:30 /dev/pts/1


To overcome this, simply allow read and write to the terminal, to all users:
john@vbox:~$ chmod o+rw /dev/pts/1
john@vbox:~$ ls -lh /dev/pts/1
crw--w-rw- 1 john tty 136, 1 Feb  12 13:32 /dev/pts/1


Once that done, you can use sudo to access the screen of the other user:
michael@vbox:~$ sudo -u john screen -r 625


Hope this help :).

Thursday, January 22, 2015

How to manage files whose name starting with hyphen (-), or double hyphen (--)

There are a few ways you can manage these kind of files, the normal way won't work, since this filename will be treated as options for almost all commands. Please see below on the method to manage these files:

Let's say the file name is -p, and you are trying to delete it,  the usual error is, since the -p is being treated as the flag for command rm, rather than a file name:

$ rm -p
rm: invalid option -- 'p'
Try 'rm ./-p' to remove the file ‘-p’.
Try 'rm --help' for more information.


So, the correct way to manage this file is:

To list:

$ ls ./-p
$ ls -- -p
$ find . -maxdepth 1 -iname "-p"


To delete:
$ rm -- -p
$ rm ./-p
$ find . -maxdepth 1 -iname "-p" -delete


To create:
$ touch ./-p


Basically, the ./ can be used with any command, while the " -- " have been tested working with ls and rm.

Hope this is helpful, thanks to stackexchange for this useful tips.

Friday, April 26, 2013

Logical AND and OR in bash

In bash (bourne again shell), the logical operand AND and OR are being symbolized by && and ||. The usage example are as follow:

&& ( If first command succeed, continue with second command, else stop )

$ ls 
cat  lion  tiger

$ ls cat && echo "there is cat"
cat
there is cat
 
$ ls elephant && echo "there is elephant"
ls: cannot access elephant: No such file or directory


You can see from above that command `echo "there is elephant"` did not get executed because the command `ls elephant` did not successfully finish (non zero exit code)


|| ( If  the first command failed, execute second command, else stop )

$ ls 
cat  lion  tiger

$ ls cat || echo "there is no cat"
cat

$ ls elephant || echo "there is no elephant"
ls: cannot access elephant: No such file or directory
there is no elephant


Now you can see that, if the first command returned non zero exit code (failed), the second command will be executed.

To run the second command, while ignoring the first command's result, use ";" instead:

$ ls tiger; echo "there is tiger"
tiger
there is tiger

$ ls elephant; echo "there is no elephant"
ls: cannot access elephant: No such file or directory
there is no elephant


That's all folks.





Wednesday, March 25, 2009

Changing default shell in ubuntu

I faced this problem when I created a new account using command line where the default shell of my new account is /bin/sh. After googling around, I found 2 useful solutions which are listed below:

1. Change user entry in /etc/passwd
a) edit /etc/passwd using any editor

  • $ vi /etc/passwd
b) find the line that belongs to the user (foo) that we about to modify
  • foo:x:1001:1001::/home/foo:/bin/sh
c) change from /bin/sh to /bin/bash
  • foo:x:1001:1001::/home/foo:/bin/bash
d) save
e) Logout and login back

2. Use chsh command
a) type chsh
  • $ chsh
b) You will be asked for password. Enter your password
c) This screen will appear
  • Enter the new value, or press ENTER for the default
    Login Shell [/bin/sh]:
d) Put /bin/bash at the menu and press Enter

Done :)