Docsity

Ch:-3,Linux,Fifth Semester,3rd Year,Bca, Study Guides, Projects, Research of Linux skills

Toggle sidebar
bg1
1. What is Shell Script? Explain how to create
and execute a Shell Script.
“A Shell Script is a text file that contains a
sequence of commands to be executed by the shell.”
It is used to automate repetitive tasks such as backups,
software installation, and system administration.
It is commonly written using Bash or sh.
Steps to Create and Execute a Shell Script
1. Create a script file:
Use the vi editor to create a file with .sh extension.
vi myscript.sh
2. Write the script:
Press i to enter insert mode and type the required
commands.
3. Save the script:
Press Esc, type :wq, and press Enter to save and exit.
4. Give execute permission:
Use the chmod command to make the script executable.
chmod +x myscript.sh
5. Execute the script:
Run the script using:
./myscript.sh
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe

Partial preview of the text

Download Ch:-3,Linux,Fifth Semester,3rd Year,Bca and more Study Guides, Projects, Research Linux skills in PDF only on Docsity!

1. What is Shell Script? Explain how to create

and execute a Shell Script.

“A Shell Script is a text file that contains a sequence of commands to be executed by the shell.”  It is used to automate repetitive tasks such as backups, software installation , and system administration.  It is commonly written using Bash or sh.

Steps to Create and Execute a Shell Script

1. Create a script file:

 Use the vi editor to create a file with .sh extension. vi myscript.sh

2. Write the script:

Press i to enter insert mode and type the required commands.

3. Save the script:

Press Esc , type :wq , and press Enter to save and exit.

4. Give execute permission:

Use the chmod command to make the script executable. chmod +x myscript.sh

5. Execute the script:

Run the script using : ./myscript.sh

Example:

#!/bin/bash echo "Hello, World!" Save it as myscript.sh, give execute permission, and run it using ./myscript.sh.

fruit="apple" case $fruit in apple) echo "It's an apple" ;; banana) echo "It's a banana" ;; *) echo "Unknown fruit" ;; esac

3. for Loop :

 The for loop is used to repeat commands for each item in a list.  It processes each value one by one.  It starts with for and ends with done. Example: for name in Ram Shyam; do echo "Hello $name" done Output : Hello Ram Hello Shyam

4. while Loop :

 The while loop repeatedly executes commands as long as the condition is true.  When the condition becomes false , the loop stops.  It ends with done. Example: count= while [ $count -le 5 ]; do echo "Count: $count" ((count++)) done

 If the command succeeds,! makes the result false; if it fails, ! makes the result true.  Example: if! grep "hello" file.txt; then echo "Text not found" fi

4. Explain Arithmetic Operations using expr

and $(( )).

 Arithmetic operations in Shell Scripting are used to perform calculations such as addition, subtraction, multiplication, division, and modulus.  In Linux shell scripting, arithmetic operations can be performed mainly using:

  1. expr command
  2. $(( )) arithmetic expansion

1. Using expr

 The expr command is an older method used to evaluate arithmetic expressions and return the result.

Syntax:

expr operand1 operator operand

Example:

a= b= result=expr $a + $b echo "Sum: $result"

Output:

Sum: 15

2. Using $(( ))

 $(( )) is the recommended and modern method for performing arithmetic operations in Bash. It is simpler, cleaner, and easier to use than expr.

Syntax:

$(( expression ))

Example:

5. Explain Filename Expansion/Wildcards: *, ?,

[].

Filename expansion , also called wildcard expansion.  It is a feature of the Linux shell used to match multiple files or directories without writing their complete names.  The shell expands the wildcard pattern before executing the command.  The main wildcards are *, ?, and [].

1. * – Asterisk

 The * wildcard matches zero or more characters in a filename.

Examples:

ls .txt Matches all files ending with .txt. ls a Matches all files starting with a. ls * Matches files and directories in the current directory.

2.? – Question Mark

 The? wildcard matches exactly one character in a filename.

Examples:

ls file?.txt It can match: file1.txt file2.txt fileA.txt

Another example: ls a?b.txt It can match filenames such as a1b.txt and acb.txt.

3. [] – Square Brackets

 The [] wildcard matches one character from the specified set or range.

Examples:

ls file[1-3].txt Matches: file1.txt file2.txt file3.txt Another example: ls file[a-c].txt Matches: filea.txt fileb.txt filec.txt

Important Points

 * → matches zero or more characters. ? → matches exactly one character.  [] → matches one character from a set or range.  Wildcards work with file and directory names , not with file contents.

6. Explain Input/Output Redirection: >, >>, <.

Redirection in Linux shell is used to send the output of a command to a file or take input from a file instead of the keyboard.  The main redirection symbols are >, >>, and <.

1. > – Output Redirection

 The > symbol is used to redirect the standard output (stdout) to a file.  If the file already exists, its previous content is overwritten.

Syntax:

command > filename

Example:

echo "Hello" > output.txt This creates output.txt and writes Hello into it. If the file already contains data, the old content is replaced.

2. >> – Append Output

 The >> symbol is used to append output to the end of a file.  It does not remove the existing content of the file.

Syntax:

command >> filename

Example:

echo "New line" >> output.txt This adds new line at the end of output.txt while keeping the existing content.

3. < – Input Redirection

 The < symbol is used to take input from a file instead of the keyboard.

Syntax:

command < filename

Example:

wc -l < input.txt This takes the contents of input.txt as input to the wc -l command and counts the number of lines.