Monthly Archives: November 2014


Bash: How to execute from root account a script as another user

There are times that you need to execute a script as another user, usually to make sure you do not mess up the access rights of the files that will be produced after the execution of the script.

An easy way to do it is using the command su (su allows to run commands with a substitute user and group ID).

An example would be to use it as su - <USER> -c "<COMMAND>
The –  <USER> will start the shell as a login shell with an environment similar to a real login.
The -c “<COMMAND>” will pass the command after -c to the newly created shell, you should wrap it in quotes if you want to pass parameters to your command.


Creating an MD5 hash of a string in bash

Make sure that you are not including the new line character (\n) as well in your string, there are cases where it might be included without you explicitly writing it.
For example if you use the output of an echo command in bash, it will automatically add a new line character to the end and will cause your hash to be not the one you would like to have.
For example, the MD5 hash of ‘bytefreaks.net’ (without the quotes) should be 16c00d9cfaef1688d4f2ddfb11b60f46 but if you execute the following you will see you will get a different result.
echo 'bytefreaks.net' | md5sum
01c46835dcb79be359e0b464ae6c6156 -

To avoid this error, use the -n option for echo that will direct the command not to output a trailing new line character.
echo -n 'bytefreaks.net' | md5sum
16c00d9cfaef1688d4f2ddfb11b60f46 -


How to list all available repositories on a Git server via ssh 12

ssh [email protected] info

The above ssh call will connect to a Git hosting server that has gitolite installed and will return the list of repositories that are available to your account along with the access rights of each.

Note: This command should work even if remote login via ssh is blocked on the server.

The command should return a list similar to this:

hello bytefreaks, this is git@git running gitolite3 v3.5.3.1-1-gf8776f5 on git 1.7.1

 R W	Repo1
 R W	Repo2
 R W	Repo3
 R  	Repo4

The first column in the results is the read flag, the second the write flag and the third column is the name of the repository.

In order to clone (get a local copy) a repository from the above list (for the example lets use Repo1) you have to issue the following command


git clone ssh://[email protected]/Repo1

To clone all of the repositories in the current directory with one command, as it is shown in this guide, issue the following command:


ssh [email protected] info | cut -f 2 | tail -n +3 | xargs -I {} -n 1 git clone ssh://[email protected]/{}


Bash script: How to check if it run by a user in terminal or not 2

We will use ‘tty’ which normally prints the file name of the connected terminal on standard input.

To make our life easier we will add the parameter -s (silent), ‘tty -s’  will print nothing and return an exit status.

tty has the following possible values for exit status:

  •  0: if standard input is a terminal
  • 1: if standard input is not a terminal
  • 2: if given incorrect arguments
  • 3: if a write error occurs

So in order to check if this script is attached to a terminal we have to do something similar to this:
tty -s;
if [ "0" == "$?" ]; then
echo "Terminal attached, you can print data as there might be a user viewing it.";
else
echo "No terminal attached, there might not be a reason to print everything.";
fi

Note: In the above script we use ‘$?’, that variable contains the return value of the last executed command, so If you are planning on using it like this, make sure you do not place any other command in between the tty -s and the if statement.