Friday, April 27, 2012

bash - regular expression approach

I just found this in the net while reading and browsing about bash, and I wanted to take this notes for my own sake :D.


#! /bin/bash

VARIABLE="Let's test this string!"

# This is for regular expressions:
if [[ "$VARIABLE" =~ "Let's.*ring" ]]
then
    echo "matched"
else
    echo "nope"
fi

# And here we have Bash Patterns:
if [[ "$VARIABLE" == L*ing! ]]
then
    echo "matched"
else
    echo "nope"
fi

Linux: View GID by Group Name

I just wanted to save this for me so that I can't forget this. For example, if you want to view the GID of group "dialout", issue the command below.

$> groupname=dialout
$> sed -nr "s/^$groupname:x:([0-9]+):.*/\1/p" /etc/group


This is helpful since there's no command like "id" can do for a group.