# Comprehensive Guide to Shell Scripting (0-1)π
Shubham Srivastava
Posted on January 21, 2024
Introduction
Welcome to the exciting world of shell scripting! This comprehensive guide will take you from the fundamentals to advanced concepts, helping you automate tasks on Linux environments using shell scripts. π
Table of Contents
- Creating and Viewing Files
- Understanding Commands
- Executing Shell Scripts
- Managing Permissions
- Useful Commands
- Example: Basic Shell Script
- Shell Scripting Roles
Creating and Viewing Files
- To create a file, use the
touch
command:touch filename
. - Shell scripts typically have the extension
.sh
. - Use
ls
to list files and directories. -
ls -ltr
sorts files by modification time.
Understanding Commands
- Use
man <command>
to get a manual about a command. - Editing files: Use
vi
to open a file,i
to insert, and:wq!
to save and exit. - The first line in a shell script should contain the shebang (
#!/bin/bash
).
Executing Shell Scripts
- To execute a shell script:
sh filename.sh
or./filename.sh
. - Permissions: Use
chmod
to grant permissions. Example:chmod 777 filename.sh
. - Permissions include read (4), write (2), and execute (1).
Managing Permissions
-
chmod 444 filename
gives read access only. - Security: Use
chmod
with root, group, and user access.
Useful Commands
-
history
: View executed commands. -
pwd
: Know the current directory. -
mkdir
: Create a directory. -
cd
: Change directory.
Example: Basic Shell Script
#!/bin/bash
# Creating a folder
mkdir user
# Creating 2 files
cd user
touch firstfile secondfile
Shell Scripting Roles
Shell scripting serves three main roles:
- Infrastructure Management
- Code Management
- Configuration Management
User-Friendly Example
Now, let's walk through an example using the username "user":
- List files in the current directory:
user@hostname:~/path/to/directory$ ls
-
Create and open a shell script file named
shellscript.sh
:
user@hostname:~/path/to/directory$ vim shellscript.sh
- List files again to confirm the creation of the script:
user@hostname:~/path/to/directory$ ls
shellscript.sh otherfile
- View the contents of the shell script:
user@hostname:~/path/to/directory$ cat shellscript.sh
#!/bin/bash
# Create a folder
mkdir user
# Create 2 files
cd user
touch firstfile secondfile
- Attempt to execute the script (permission denied):
user@hostname:~/path/to/directory$ ./shellscript.sh
bash: ./shellscript.sh: Permission denied
- Grant execute permission to the script:
user@hostname:~/path/to/directory$ chmod 777 shellscript.sh
- Execute the script successfully:
user@hostname:~/path/to/directory$ ./shellscript.sh
- List files to verify changes:
user@hostname:~/path/to/directory$ ls
shellscript.sh user otherfile
- Navigate to the 'user' directory:
user@hostname:~/path/to/directory$ cd user
-
List files in the 'user' directory:
user@hostname:~/path/to/directory/user$ ls firstfile secondfile
-
Attempt to execute the script inside the 'user' directory (file not found):
user@hostname:~/path/to/directory/user$ ./shellscript.sh bash: ./shellscript.sh: No such file or directory
-
Navigate back to the parent directory:
user@hostname:~/path/to/directory/user$ cd ..
-
Attempt to execute the script again (folder already exists):
user@hostname:~/path/to/directory$ ./shellscript.sh mkdir: cannot create directory βuserβ: File exists
π πͺ π
π©
Shubham Srivastava
Posted on January 21, 2024
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.