Linux

Creating your first Linux scripts

In this video, we will create several simple Linux scripts. While simple, these scripts highlight how to automate many of the tasks you may have for your environment.

HelloWorld.sh

The first script, HelloWorld.sh is created by using your favorite Linux text editor (I suggest nano or vi). The first line is the “shebang” or “hashbang” that is used by the system to identify which application to run the script with. In this case it is a bash script, but there are also perl, python, and other scripting languages, so the shebang is helpful to identify the correct script type.
The next two lines start with a pound sign or hash tag (#). These lines are comments and the pound sign is a way of telling the computer to ignore these lines. Comments can and should be used extensively to ensure future readers, including yourself, understand the goal and purpose of the lines in your script.
Lastly, the echo command simply writes out “Hello World!”.

#!/bin/bash
# Created by Ed on 7/16/19
# Writes out Hello

echo “Hello World!”

Once the file is created, it needs to be marked as executable. We do this by running chmod +x HelloWorld.sh from the command line. Once it is marked as executable, we can run it by typing ./HelloWorld.sh (note the leading dot and slash before the name).

HelloVar.sh

This second script operates similar to the first, but uses variables. The script begins with the shebang and a few lines of comments. The sixth line assigns “HelloWorld!” to the variable named var – note that there are no spaces around the equal sign, this is important. Once the variable is assigned, we use echo to write out the value.
After we have shown how we can use variables, we then assign 2 more variables: timenow and computername. The commands date and hostname are called and the outputs are assigned to the appropriate variables. Once assigned, we finally use echo to write out the values, along with some descriptive text.

#!/bin/bash
# Created by Ed on 7/16/19
# Writes out Hello using variables

# Set the variable
var=”Hello World!”

# Print out variable
echo “$var”

# Assign more variables
timenow=”$(date)”
computername=”$(hostname)”

# Write out other variables
echo “Current date and time: $timenow”
echo “Computer name: $computername”

Again, once saved we use chmod +x HelloVar.sh to mark the file as executable. Then we can call the script by typing ./HelloVar.sh from the command line.

HelloInput.sh

This third script uses the read command to request input from the user. The script starts with the shebang and several lines of comments before calling the read command. The read command will read in a single line of data from the user, and then save it into the variable name. The -p parameter tells read to echo out a prompt to the user, and then wait for input.
 
#!/bin/bash
# Created by Ed on 7/16/19
# Prompts user for input and outputs hello

# Ask the users name
read -p “Please tell me your name: ” name

# Say hello
echo “Hello $name, would you like to play a game?”

When finished, set it as executable and run it. The script will ask for your name and then respond back by saying Hello specifically to you

——— Challenge ——— 
Create a script that:
1. Prompts for a username
2. Creates the user
3. Sets the password to Password01 (see examples below)
   echo -e “‘Password01’n’Password01′” | passwd Tim -f
OR
   echo “Password01” | passwd –stdin Tim
4. Adds user to Marketing group

Save script as NewMarketingUser.sh
Run script as root (sudo) to confirm

——— Challenge 2 ———
Create a script that:
1. Prompts for a group name
2. Creates the group
3. Creates a group folder in /usr
4. Sets group permissions on folder

Save script as NewGroup.sh
Run script as root (sudo) to confirm

Leave a Reply