Shell script


Using "if" statement checks for a file called "student.txt" in the current directory. Removes (deletes) the file if it exist. This file will collect last name, first name, and grade for 5 people.

Use a while loop to get the information (last name, first name, and age) for 5 people. grade should be 0-100. Use three different variable names (fname, lname, and grade) to read the data from the keyboard.

Store the data read from the keyboard in a file called "student.txt". After reading all the data for 5 people, display contents of name.txt file on the monitor.

Below is my attempt

if [ -f name.txt ]; then
# I put something in here and it'll be done if the file exists
fi

person_num=1
while [ $person_num -lt 6 ]; do
# put something here to repeat for each person

-p fname
-s name.txt
-p lname
-s name.txt
-p age
-s name.txt
person_num = $(eval $person_num + 1)
done

if[$person_num ==6]
# read name.txt file
-r name.txt

Here's the corrected version of your shell script:

```shell
#!/bin/bash

if [ -f student.txt ]; then
rm student.txt
fi

person_num=1

while [ $person_num -lt 6 ]; do
echo "Enter the first name: "
read fname
echo $fname >> student.txt

echo "Enter the last name: "
read lname
echo $lname >> student.txt

echo "Enter the grade (0-100): "
read grade
echo $grade >> student.txt

person_num=$((person_num + 1))
done

cat student.txt
```

Explanation:

1. The script starts with a shebang (`#!/bin/bash`) to specify the interpreter as Bash.
2. The `if` statement checks if the file "student.txt" exists, and if it does, it deletes it using the `rm` command.
3. The variable `person_num` is initialized to 1.
4. The `while` loop is used to repeat the input process for each person until `person_num` reaches 6.
5. Inside the loop, the user is prompted to enter the first name, last name, and grade using the `read` command, and the data is appended to the file "student.txt".
6. The `person_num` is incremented by 1 using the `person_num=$((person_num + 1))` syntax.
7. After the loop ends, the contents of the "student.txt" file are displayed on the screen using the `cat` command.

Make sure to save the script with a ".sh" extension, give it execute permissions (`chmod +x script.sh`), and run it using `./script.sh` command.

Here is a modified version of your shell script:

```bash
#!/bin/bash

# Check if student.txt file exists and remove it if it does
if [ -f student.txt ]; then
rm student.txt
fi

person_num=1
while [ $person_num -lt 6 ]; do
# Prompt for first name
read -p "Enter first name: " fname
echo "$fname" >> student.txt

# Prompt for last name
read -p "Enter last name: " lname
echo "$lname" >> student.txt

# Prompt for grade (0-100)
read -p "Enter grade (0-100): " grade
# Check if grade is within the valid range
if ! [[ "$grade" =~ ^[0-9]+$ ]] || ((grade < 0 || grade > 100)); then
echo "Invalid grade entered. Please enter a number between 0 and 100."
continue
fi
echo "$grade" >> student.txt

person_num=$((person_num + 1))
done

# Display contents of student.txt file on the monitor
cat student.txt
```

Explanation:

1. First, we check if the `student.txt` file exists using the `-f` conditional expression. If it exists, we remove it using the `rm` command.
2. Next, we initialize the `person_num` variable to 1.
3. We use a `while` loop to prompt for the information (first name, last name, and grade) for each person.
4. Inside the loop, we use the `read` command to prompt for and read the values of `fname`, `lname`, and `grade` from the keyboard. The values entered are then appended to the `student.txt` file using `echo`.
5. We check if the entered `grade` is a valid number between 0 and 100 using a regular expression and arithmetic comparison. If it's not valid, an error message is displayed and the loop continues to the next iteration.
6. After reading the data for all 5 people, we display the contents of the `student.txt` file on the monitor using the `cat` command.

Save the script in a file (e.g., `student.sh`), make it executable (`chmod +x student.sh`), and then execute it (`./student.sh`) to run the script.