0% found this document useful (0 votes)
51 views4 pages

Unix/Linux Shell Scripting Guide

Uploaded by

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

Unix/Linux Shell Scripting Guide

Uploaded by

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

Unix/Linux and Shell Scripting - Extended Full Notes

Generated on: 2025-04-20

Unix/Linux and Shell Scripting - Full Notes

1. Introduction to Unix and Linux

- Unix: Multiuser, multitasking OS developed at Bell Labs.

- Linux: Open-source Unix-like OS created by Linus Torvalds.

- Differences: Linux is open-source and widely used in modern systems.

- Kernel, Shell, Utilities, Application Programs.

2. File System and Directory Management

- Filesystem is hierarchical, starting from root (/)

- Directories: /bin, /home, /etc, /usr, /var, etc.

- Commands:

cd, pwd, ls, mkdir, rmdir, touch, rm, cp, mv

- File Permissions:

- rwxr-xr--

- chmod, chown, chgrp

3. Basic Shell Scripting Concepts

- Shell: Interface between user and kernel (bash, sh)

- Script: File with commands

#!/bin/bash

echo "Hello, World!"


- Variables:

name="Hemanth"

echo $name

4. Advanced Shell Commands and Utilities

- grep: Search text

grep "word" [Link]

- sed: Stream editor

sed 's/old/new/g' [Link]

- awk: Text processing

awk '{print $1}' [Link]

- sort, uniq, head, tail, cut, tr

5. Pipes and Redirection

- >, >>, <, |

- ls > [Link]

- cat [Link] | grep "text"

6. vi/vim Editor

- Modes: Command, Insert, Visual

- i to insert, Esc to return, :wq to save and quit

- Basic commands: dd, yy, p, u

7. Process and Job Management

- ps, top, kill, bg, fg, jobs

- nohup, nice, renice


- kill -9 PID

8. Network Commands

- ping, netstat, ssh, scp, ftp, curl, wget

9. Conditional Statements

if [ $num -gt 0 ]; then

echo "Positive"

elif [ $num -lt 0 ]; then

echo "Negative"

else

echo "Zero"

fi

10. Looping Constructs

- for i in 1 2 3; do echo $i; done

- while [ $x -le 5 ]; do echo $x; ((x++)); done

- until [ $x -gt 5 ]; do echo $x; ((x++)); done

11. Practical Script Example

#!/bin/bash

echo "Backup starting..."

tar -czf backup_$(date +%F).[Link] /home/user/

echo "Done!"

12. Useful Built-in Variables


$0, $1, $#, $*, $?, $$

13. Functions in Shell

function say_hello() {

echo "Hello $1"

say_hello Hemanth

14. Debugging Scripts

- Use `set -x` and `set +x` to enable/disable debugging

- Use `bash -x [Link]` to trace

15. Scheduling with cron

- Edit with `crontab -e`

- Format: * * * * * /path/to/[Link]

- List: crontab -l

16. Best Practices

- Comment your scripts

- Use quotes to prevent globbing

- Use functions for modularity

Common questions

Powered by AI

The vi/vim editor uses modes like Command, Insert, and Visual to streamline editing tasks. Command mode allows navigation and operations such as 'dd', 'yy', and 'p' without leaving the home row keys, improving efficiency. Insert mode facilitates text input, and Visual mode aids in block operations. Such features support rapid, keyboard-centric text editing .

Shell scripting automates repetitive tasks, reduces manual intervention, and streamlines processes using command syntax within files. Examples include using variables (name="Hemanth"), conditional statements (if [ $num -gt 0 ]; then echo "Positive"), and looping constructs (for i in 1 2 3; do echo $i; done) to automate system administration or batch processing .

File permissions in Unix/Linux, such as -rwxr-xr--, define the read, write, and execute rights for the owner, group, and others, thus enhancing security by restricting unauthorized access or modifications. The 'chmod' command changes the permissions, 'chown' changes file ownership, and 'chgrp' changes the group association of a file .

Linux is preferred in modern computing due to its open-source nature, allowing for extensive customization and community-driven developments. Unix, developed at Bell Labs, is more closed in comparison. While both are multiuser and multitasking operating systems, Linux's widespread adoption in servers and desktops stems from its cost-effectiveness and adherence to open-source principles .

These advanced utilities greatly enhance text processing. 'grep' searches and filters text using regular expressions, 'sed' serves as a stream editor to transform text based on patterns, and 'awk' processes data and reports by applying operations per line. Together, they enable complex text manipulations, making shell scripting powerful for handling large datasets .

Commands like 'ssh' and 'scp' are integral for secure remote system administration and data transfer. 'ssh' establishes encrypted connections for remote login and command execution, while 'scp' securely copies files between hosts over a network. Their use ensures data integrity and security during remote operations .

Cron allows for periodic task execution by scheduling scripts via crontab, contributing to system automation and reducing the need for manual intervention. The basic syntax in a crontab file follows the format '* * * * * /path/to/script.sh', defining minutes, hours, days of the month, months, and days of the week for script execution .

Pipes (|) and redirection (>, >>, <) enable the linking of commands in Unix/Linux, allowing output from one command to serve as input to another. For instance, 'ls > list.txt' directs the output of 'ls' to a file, and 'cat file.txt | grep "text"' passes the content of a file to 'grep' for searching. These tools facilitate complex command sequencing efficiently .

Conditional statements like 'if-elif-fi' and loops such as 'for', 'while', and 'until' are crucial for dynamic decision-making and repetitive task execution in scripts. An example is checking a numerical condition with 'if [ $num -gt 0 ]; then echo "Positive"', and iterating through numbers with 'for i in 1 2 3; do echo $i; done'. These constructs convert static scripts into adaptable problem-solving tools .

Commands like 'ps' and 'top' monitor processes, while 'kill', 'bg', and 'fg' manage job states, assisting in effective multitasking by prioritizing or terminating tasks. 'nohup' ensures processes continue running even after logout, and 'nice' adjusts scheduling priorities, aiding in optimal resource allocation within the multitasking environment .

You might also like