Category Archives: linux bash

Automatically creating a user and forcing password change on first login: A Linux scripting example

Sometimes there might be a need to make a test user really fast. Reasons may vary and we might want to make this as easy as possible for ourselves. We are of course thinking about adding our test user to sudo/wheel group and we have a our default password at mind that we will set to our test user.

However, we might want to force user to change the password during first login and be certain that our poorly chosen default password will not stay universal on possibly critical systems. Later, we also might want to lock the user account and possibly even delete it when there is no need to test anymore. Here is how do all this with some good old scripting:

https://github.com/postman721/scripting-examples/blob/main/auto_creation.sh

Bash: File content tricks

Using bash to get some data from the files can be very useful. Here are some essential tricks to get you started.

  1. See if the file has a word called “hello”:

cat file.txt |grep hello

  1. Compare two files using cmp / cmp is usually part of the diffutils package.

This part is best done with simple script. However, scripting is not mandatory when using cmp.

#!/bin/bash

# Creating two new files with: echo "Helllo" > file.txt && echo "Hello" > file2.txt

file1="file.txt"
file2="file2.txt"

if cmp "$file1" "$file2"; then
echo "Files are the same."
else
echo "Files are not the same."
fi

Continue reading