Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2 views

Shell Script program

The document contains several shell scripts for basic arithmetic operations, checking if a number is even or odd, and categorizing Linux file types. It also includes troubleshooting steps for script errors and issues with file permissions. Additionally, it discusses copying skeleton files for user customization in Cygwin and provides examples of common errors encountered while executing shell scripts.

Uploaded by

vikas sharma
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Shell Script program

The document contains several shell scripts for basic arithmetic operations, checking if a number is even or odd, and categorizing Linux file types. It also includes troubleshooting steps for script errors and issues with file permissions. Additionally, it discusses copying skeleton files for user customization in Cygwin and provides examples of common errors encountered while executing shell scripts.

Uploaded by

vikas sharma
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Script program

Here's a simple addition program in shell script for you:

#!/bin/bash
# Program to add two numbers

echo "Enter first number: "


read a
echo "Enter second number: "
read b

sum=$((a + b))

echo "The sum of $a and $b is: $sum"

simple shell script to check if a number is even or odd:


#!/bin/bash
# Program to check if a number is even or odd

echo "Enter a number: "


read num

if [ $((num % 2)) -eq 0 ]; then


echo "$num is an even number."
else
echo "$num is an odd number."
fi

run sh evenodd.sh

shell script that performs basic arithmetic operations (addition, subtraction, multiplication,
and division) on two numbers:

#!/bin/bash
# Program to perform basic arithmetic operations

echo "Enter first number: "


read num1
echo "Enter second number: "
read num2

echo "Choose an operation:"


echo "1. Addition"
echo "2. Subtraction"
echo "3. Multiplication"
echo "4. Division"
read choice

case $choice in
1)
result=$((num1 + num2))
echo "The sum of $num1 and $num2 is: $result"
;;
2)
result=$((num1 - num2))
echo "The difference between $num1 and $num2 is: $result"
;;
3)
result=$((num1 * num2))
echo "The product of $num1 and $num2 is: $result"
;;
4)
if [ $num2 -ne 0 ]; then
result=$((num1 / num2))
echo "The quotient when $num1 is divided by $num2 is: $result"
else
echo "Division by zero is not allowed."
fi
;;
*)
echo "Invalid choice. Please select a valid operation."
;;
esac

Linux file and types


Linux files are categorized into several different types based on their purpose and structure.
Here are the primary types of files you will encounter in a Linux system:

1. Regular Files:
o These are the most common types of files, which contain data, text, or
program instructions.
o Examples: Documents, images, executables, scripts, etc.
2. Directory Files:
o These files contain information about other files and directories.
o They are used to organize files into a hierarchical structure.
o Examples: /home, /var/log, etc.
3. Character Device Files:
o These files represent devices that handle data as a stream of characters, such as
keyboards and mice.
o Examples: /dev/tty0, /dev/console, etc.
4. Block Device Files:
o These files represent devices that handle data in blocks, such as hard drives
and USB drives.
o Examples: /dev/sda, /dev/sdb, etc.
5. FIFO (Named Pipe) Files:
o These files are used for inter-process communication (IPC), allowing data to
be passed between processes.
o FIFO stands for "First In, First Out."
o Example: A named pipe created with the mkfifo command.
6. Socket Files:
o These files are used for network communication between processes.
o Examples: Unix domain sockets and network sockets.
7. Symbolic Links:
o These are files that point to other files or directories.
They act as shortcuts or aliases.
o
Example: A symbolic link created with the ln -s command.
o
8. Hard Links:
o These are additional directory entries pointing to the same inode as an existing
file.
o Unlike symbolic links, they do not break if the original file is deleted.
o Example: A hard link created with the ln command.

Linux uses the ls -l command to display file types, indicated by the first character of the
file permissions:

 -: Regular file
 d: Directory
 c: Character device file
 b: Block device file
 p: FIFO file
 s: Socket file
 l: Symbolic link

Let me know if you have more questions or need further clarification!

 Place the cursor in the last cell of the table and press Ctrl + Enter to insert a
paragraph break. This should move the cursor outside the table.

Fgg
Copying skeleton files.
These files are for the users to personalise their cygwin experience.

They will never be overwritten nor automatically updated.

'./.bashrc' -> '/home/HP//.bashrc'


'./.bash_profile' -> '/home/HP//.bash_profile'
'./.inputrc' -> '/home/HP//.inputrc'
'./.profile' -> '/home/HP//.profile'

HP@DESKTOP-4NEDODV ~
$ vi my.sh
E1187: Failed to source defaults.vim
Press ENTER or type command to continue

HP@DESKTOP-4NEDODV ~
$ ./my.sh
-bash: ./my.sh: Permission denied

HP@DESKTOP-4NEDODV ~
$ chmod 555 my.sh

HP@DESKTOP-4NEDODV ~
$ ./my.sh
enter no
5
./my.sh: line 6: syntax error near unexpected token `else'
./my.sh: line 6: `else'

HP@DESKTOP-4NEDODV ~
$ vi my.sh
E1187: Failed to source defaults.vim
Press ENTER or type command to continue

HP@DESKTOP-4NEDODV ~
$ chmod 777 my.sh

HP@DESKTOP-4NEDODV ~
$ vi my.sh
E1187: Failed to source defaults.vim
Press ENTER or type command to continue

HP@DESKTOP-4NEDODV ~
$ ./my.sh
enter no
6
./my.sh: line 6: syntax error near unexpected token `fi'
./my.sh: line 6: `fi'

HP@DESKTOP-4NEDODV ~
$ vi my.sh
E1187: Failed to source defaults.vim
Press ENTER or type command to continue

HP@DESKTOP-4NEDODV ~
$ ./my.sh
enter no
6
./my.sh: line 4: [: missing `]'
HP@DESKTOP-4NEDODV ~
$ cat my.sh

echo "enter no"


read a
if [ $a == 0]
then
echo " zero"
fi

HP@DESKTOP-4NEDODV ~
$ vi my.sh
E1187: Failed to source defaults.vim
Press ENTER or type command to continue

HP@DESKTOP-4NEDODV ~
$ ./my.sh
enter no
6
./my.sh: line 4: [: missing `]'

HP@DESKTOP-4NEDODV ~
$ vi my.sh
E1187: Failed to source defaults.vim
Press ENTER or type command to continue

HP@DESKTOP-4NEDODV ~
$ ./my.sh
enter no
5
./my.sh: line 4: [: missing `]'

HP@DESKTOP-4NEDODV ~
$ bash my.sh
enter no
6
my.sh: line 4: [: missing `]'

HP@DESKTOP-4NEDODV ~
$ pwd
/home/HP

HP@DESKTOP-4NEDODV ~
$ mkdir ubuntuTerminal_Program

HP@DESKTOP-4NEDODV ~
$ pwd
/home/HP

HP@DESKTOP-4NEDODV ~
$ cd ubuntuTerminal_Program/

HP@DESKTOP-4NEDODV ~/ubuntuTerminal_Program
$ pwd
/home/HP/ubuntuTerminal_Program

HP@DESKTOP-4NEDODV ~/ubuntuTerminal_Program
$ ls
'New Text Document.txt' mm.sh

HP@DESKTOP-4NEDODV ~/ubuntuTerminal_Program
$ bash mm.sh
': not a valid identifier
mm.sh: line 3: $'\r': command not found
mm.sh: line 4: $'\r': command not found
mm.sh: line 7: syntax error near unexpected token `elif'
'm.sh: line 7: `elif [ $num -lt 0 ]; then

HP@DESKTOP-4NEDODV ~/ubuntuTerminal_Program
$ cat mm.sh
echo -n “Enter a number: “
read num

if [ $num -gt 0 ]; then


echo “$num is a positive number.”
elif [ $num -lt 0 ]; then
echo “$num is a negative number.”
else
echo “$num is zero.”
fi

HP@DESKTOP-4NEDODV ~/ubuntuTerminal_Program
$ cat mm.sh
echo -n "Enter a number: "
read num

if [ $num -gt 0 ]; then


echo "$num is a positive number."
elif [ $num -lt 0 ]; then
echo "$num is a negative number."
else
echo "$num is zero."
fi

HP@DESKTOP-4NEDODV ~/ubuntuTerminal_Program
$ bash mm.sh
': not a valid identifier
mm.sh: line 3: $'\r': command not found
mm.sh: line 4: $'\r': command not found
mm.sh: line 7: syntax error near unexpected token `elif'
'm.sh: line 7: `elif [ $num -lt 0 ]; then

HP@DESKTOP-4NEDODV ~/ubuntuTerminal_Program
$ vim mm.sh
-bash: vim: command not found

HP@DESKTOP-4NEDODV ~/ubuntuTerminal_Program
$ vi mm.sh
E1187: Failed to source defaults.vim
Press ENTER or type command to continue

HP@DESKTOP-4NEDODV ~/ubuntuTerminal_Program
$ bash mm.sh
Enter a number:
': not a valid identifier
mm.sh: line 3: $'\r': command not found
mm.sh: line 4: $'\r': command not found
mm.sh: line 7: syntax error near unexpected token `elif'
'm.sh: line 7: `elif [ $num -lt 0 ]; then

HP@DESKTOP-4NEDODV ~/ubuntuTerminal_Program
$ bash mm.sh
': not a valid identifier
mm.sh: line 3: $'\r': command not found
mm.sh: line 4: $'\r': command not found
mm.sh: line 7: syntax error near unexpected token `elif'
'm.sh: line 7: `elif [ $num -lt 0 ]; then
HP@DESKTOP-4NEDODV ~/ubuntuTerminal_Program
$ cat mm.sh
echo -n "Enter a number "
read num

if [ $num -gt 0 ]; then


echo "$num is a positive number."
elif [ $num -lt 0 ]; then
echo "$num is a negative number."
else
echo "$num is zero."
fi

HP@DESKTOP-4NEDODV ~/ubuntuTerminal_Program
$ echo $0
-bash

HP@DESKTOP-4NEDODV ~/ubuntuTerminal_Program
$ vi my.sh
E1187: Failed to source defaults.vim
Press ENTER or type command to continue

HP@DESKTOP-4NEDODV ~/ubuntuTerminal_Program
$ ls
'New Text Document.txt' mm.sh

HP@DESKTOP-4NEDODV ~/ubuntuTerminal_Program
$ vi mm.sh
E1187: Failed to source defaults.vim
Press ENTER or type command to continue

HP@DESKTOP-4NEDODV ~/ubuntuTerminal_Program
$ ./mm.sh
Enter a number
': not a valid identifierum
./mm.sh: line 3: $'\r': command not found
./mm.sh: line 4: $'\r': command not found
./mm.sh: line 7: syntax error near unexpected token `elif'
'/mm.sh: line 7: `elif [ $num -lt 0 ]; then

HP@DESKTOP-4NEDODV ~/ubuntuTerminal_Program
$ vi mm.sh
E1187: Failed to source defaults.vim
Press ENTER or type command to continue

HP@DESKTOP-4NEDODV ~/ubuntuTerminal_Program
$ ./mm.sh
Enter a number
': not a valid identifierum
./mm.sh: line 4: $'\r': command not found
./mm.sh: line 5: $'\r': command not found
./mm.sh: line 9: syntax error near unexpected token `elif'
'/mm.sh: line 9: `elif [ $num -le 0 ];

HP@DESKTOP-4NEDODV ~/ubuntuTerminal_Program
$ ^C

HP@DESKTOP-4NEDODV ~/ubuntuTerminal_Program
$ dos2unix ./mm.sh
-bash: dos2unix: command not found

HP@DESKTOP-4NEDODV ~/ubuntuTerminal_Program
$ dos2unix mm.sh
-bash: dos2unix: command not found
HP@DESKTOP-4NEDODV ~/ubuntuTerminal_Program
$ sudo apt update && sudo apt upgrade
-bash: sudo: command not found

HP@DESKTOP-4NEDODV ~/ubuntuTerminal_Program
$ x=7

HP@DESKTOP-4NEDODV ~/ubuntuTerminal_Program
$x
-bash: x: command not found

HP@DESKTOP-4NEDODV ~/ubuntuTerminal_Program
$ echo " $x"
7

HP@DESKTOP-4NEDODV ~/ubuntuTerminal_Program
$ if [ $x=7 ]; then echo "equal 7"; else echo "does not equal u"; fi
equal 7

HP@DESKTOP-4NEDODV ~/ubuntuTerminal_Program
$ cat mm.sh
#!/bin/bash
echo "Enter a number "
read num

if [ $num -ge 0 ];
then
echo "$num is a positive number."
elif [ $num -le 0 ];
then
echo "$num is a negative number."
else
echo "$num is zero."
fi

HP@DESKTOP-4NEDODV ~/ubuntuTerminal_Program
$ vi mm.sh
E1187: Failed to source defaults.vim
Press ENTER or type command to continue

HP@DESKTOP-4NEDODV ~/ubuntuTerminal_Program
$ ./mm.sh
Enter a number
': not a valid identifierum
./mm.sh: line 4: $'\r': command not found
./mm.sh: line 5: $'\r': command not found
./mm.sh: line 9: syntax error near unexpected token `elif'
'/mm.sh: line 9: `elif [ $num < 0 ];

HP@DESKTOP-4NEDODV ~/ubuntuTerminal_Program
$ vi mm.sh
E1187: Failed to source defaults.vim
Press ENTER or type command to continue

HP@DESKTOP-4NEDODV ~/ubuntuTerminal_Program
$ ./mm.sh
Enter a number
': not a valid identifierum
./mm.sh: line 4: $'\r': command not found
./mm.sh: line 5: $'\r': command not found
./mm.sh: line 17: syntax error: unexpected end of file

HP@DESKTOP-4NEDODV ~/ubuntuTerminal_Program
$ vi mm.sh
E1187: Failed to source defaults.vim
Press ENTER or type command to continue

HP@DESKTOP-4NEDODV ~/ubuntuTerminal_Program
$ ./mm.sh
Enter a number
': not a valid identifierum
./mm.sh: line 4: $'\r': command not found
./mm.sh: line 5: $'\r': command not found
./mm.sh: line 17: syntax error: unexpected end of file

HP@DESKTOP-4NEDODV ~/ubuntuTerminal_Program
$ u2d ./mm.sh
-bash: u2d: command not found

HP@DESKTOP-4NEDODV ~/ubuntuTerminal_Program
$

Dfdf

You might also like