HowTos


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.


How to: remove prefix and suffix from a variable in bash 3

string="/scripts/log/mount_hello_kitty.log";
prefix="/scripts/log/mount_";
string=${string#$prefix}; #Remove prefix
suffix=".log";
string=${string%$suffix}; #Remove suffix
echo $string; #Prints "hello_kitty"

In the above example we have as input the variable string that contains following /scripts/log/mount_hello_kitty.log.

We want to remove from that variable the prefix, which is /scripts/log/mount_ and the suffix, which is .log.

The above code will replace in place the input that is contained in the variable string and remove the prefix and suffix we defined in the respective variables.

string=${string#$prefix} returns a copy of string after the prefix was removed from the beginning of the variable.

string=${string%$suffix} returns a copy of string after the suffix was removed from the end of the variable.


How to: Extract all usernames that are logged in from who

who | cut -d ' ' -f 1 | sort -u

who: will show who is logged on

cut  -d ‘ ‘ -f 1: will remove all sections from each line except for column 1. It will use the space character as the delimiter for the columns

sort -u: it will sort the usernames and remove duplicate lines. So if a user is logged in multiple times you will get that username only once.
In case you want to filter out root user from this list you can do it as follows:

who | cut -d ' ' -f 1 | sort -u | grep -v 'root'