GNU/Linux


Bash: Read a file, line by line

The following script will accept from the keyboard a filename, later it will read it and process it line by line.
In this example we will just number the lines, print them and count the total number of lines in the file.

#!/bin/sh
echo Enter the Filename to process
read filename
exec<$filename
lineNumber=0
while read line
do
	lineNumber=`expr $lineNumber + 1`;
	echo $lineNumber - $line;
done
echo "Total Lines Number: $lineNumber";


Remove leading characters/information from line using sed

Remove leading numbering from line

We had these log files that on most lines at the beginning there was a number followed by a dot and some times it had space characters.
The following sed command removes that prefix. and leaved intact the rest of the lines that do not have the prefix.

cat $log | sed '/^[0-9]*. */!d; s///;q';

e.g

Input: 123. Some text here.
Output:Some text here.

Remove leading whitespace (space and tabs) from line:

The following sed script will remove all leading whitespace from each line by using a regular expression to match space characters and tabs.

cat $log | sed 's/^[ t]*//';

e.g

Input:       Some text here.
Output:Some text here.

Linux: Check if a User or a Group Exists 2

You can find out if user exists by searching in the /etc/passwd file using the following command:

egrep -i "^useraccount:" /etc/passwd

The above command will print the matching record from /etc/passwd if the user exists or nothing if the user does not exist.
The ^ symbol is used to make sure there is no characters before the username and the : character is used as the delimiter in the file (which indicates the end of the username). By wrapping the username with these characters we are sure that if we matched a record, we matched the correct record with the full username.

A very simple way to use this code in a script is by utilizing the $? (question mark) variable. The question mark variable contains the exit status of the last command that executed. Specifically, egrep will return 0 if there was a match or else it will return a a positive number (usually 1).
Taking advantage of this behavior, after executing the above command, we check the $? variable to see the result with an if statement.

egrep -i "^useraccount:" /etc/passwd;
if [ $? -eq 0 ]; then
   echo "User Exists"
else
   echo "User does not exist -- Invalid Username"
fi

You can also find out if a group exists by searching in the /etc/group file. Similar to the approach we showed before, we can check if a group exists using the following:

egrep -i "^groupname" /etc/group;
if [ $? -eq 0 ]; then
   echo "Group Exists"
else
   echo "Group does not exist -- Invalid Group name"
fi