0% found this document useful (0 votes)
5 views2 pages

Essential Shell Script Commands Guide

The document provides a series of shell script commands and tasks for various operations, such as printing the username and home directory, greeting a user, displaying the current working directory, and performing arithmetic operations. It also includes commands for file manipulation, system information retrieval, and user interaction. Each task is presented with the corresponding echo statements and commands to achieve the desired output.

Uploaded by

anuv
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views2 pages

Essential Shell Script Commands Guide

The document provides a series of shell script commands and tasks for various operations, such as printing the username and home directory, greeting a user, displaying the current working directory, and performing arithmetic operations. It also includes commands for file manipulation, system information retrieval, and user interaction. Each task is presented with the corresponding echo statements and commands to achieve the desired output.

Uploaded by

anuv
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

1.

Print your username and home directory

echo "Username: $USER"


echo "Home directory: $HOME"

2. Write a shell script that takes a user's name as input and greets them.

echo "What's your name?"


read name
# Greet the user
echo "Hello, $name! Nice to meet you."

3. Display current working directory and List all files in the current directory

echo "Current directory: $(pwd)"


echo "Files in current directory:" ls

4. Read two numbers and print their sum

echo "Enter first number:"


read a
echo "Enter second number:"
read b
sum=$((a + b))
echo "Sum = $sum"

5. Display the hostname of the system

echo "Hostname: $(hostname)"

6. Display the current shell

echo "Current shell: $SHELL"

7. Display the number of logged in users

echo "Number of logged-in users: $(who | wc -l)"

8. Show the contents of a file entered by the user

echo "Enter file name:"


read file
cat "$file"

9. Display disk usage of your system

echo "Disk usage:"


df -h

10: Display your system’s IP address

echo "IP address:"


hostname -I
Q11: Create a directory whose name is entered by the user

echo "Enter directory name:"


read dname
mkdir "$dname"
echo "Directory $dname created."
Q12: Create an empty file with the name entered by the user

echo "Enter file name to create:"


read fname
touch "$fname"
echo "File $fname created."

Q13: Display the size of a given file

echo "Enter file name:"


read fname
stat --format="%s bytes" "$fname"

Q14: Display the path of a command (use which)

echo "Enter command name:"


read cmd
which "$cmd"

Q15: Print the first argument passed to the script on the command line

echo "First argument: $1"

Q16: Print all command line arguments given to the script

echo "All arguments: $@"

Common questions

Powered by AI

The shell script demonstrates basic user interaction by prompting the user for their name and greeting them accordingly . It retrieves system information by displaying the current working directory, listing files, showing the hostname, current shell, and number of logged users. These commands offer a simple yet comprehensive overview of how scripts can be used to interact with both users and the system .

The combination of `df -h` and `hostname -I` provides a snapshot of both the disk usage in human-readable form and the network address configuration. This dual perspective aids system maintenance by allowing administrators to assess hardware resource usage alongside network accessibility and reachability, which are critical for ensuring system performance and connectivity align with operational needs .

A shell script can perform mathematical operations by reading numeric inputs and using arithmetic expansion with `$((expression))`. For instance, the script reads two numbers and calculates their sum using `sum=$((a + b))`, demonstrating how scripts can automate basic calculations and present results without any external calculator tool .

Using `$(pwd)` retrieves and displays the current working directory, providing context for the subsequent use of `ls`, which lists all files within that directory. Together, they give a complete snapshot of the user's current directory state, enhancing navigational clarity and management of files from the command line .

Shell scripts offer automation, repeatability, and efficiency, significantly reducing manual effort in creating and managing files and directories. Commands like `mkdir` and `touch` are executed quickly within scripts, enabling batch processing, reducing errors from manual command repetition, and facilitating easier integration into larger automated workflows for managing complex environments .

The script examples illustrate the manipulation and use of command line arguments by printing them with `echo "First argument: $1"` and `echo "All arguments: $@"` . This shows how scripts can be made flexible and reusable, accepting different inputs without changing the script's core, thus maximizing efficiency for variably repetitive tasks .

The approach used is `cat "$file"`, which reads and displays the content of the file specified by the user . Potential risks include inadvertently displaying sensitive information if users input a file containing confidential data, emphasizing the need for careful handling and perhaps additional security measures, like input validation, in environments dealing with sensitive files .

The script employs `stat --format="%s bytes" "$fname"` to measure and display the file's size in bytes . Knowing file sizes is crucial for storage management as it helps in monitoring disk space usage, planning for capacity needs, and ensuring that applications or processes do not exceed storage quotas or thresholds .

The command used is `mkdir "$dname"` which creates a directory with the name entered by the user. This illustrates command-line interaction capabilities by allowing users to influence the script's operation directly through input, demonstrating the dynamic nature of shell scripting .

The script uses `hostname -I` to retrieve the machine's IP address and `$(hostname)` to display the hostname . These identifiers are crucial in networking diagnostics as they help in checking network configurations, resolving connectivity issues, and confirming whether a device is correctly joined to a network, thus aiding in administrative tasks for network maintenance .

You might also like