Bash Scripting Help: How to Find Students Who Haven't Submitted Files?

  • Thread starter caelestis
  • Start date
In summary, this script uses the grep and cut commands to extract student IDs from both the database and log files. It then loops through each student ID from the database and checks if it is present in the log file. If the student has not submitted any files, their name will be printed. This script can be executed with the names of the two files as command line arguments.
  • #1
caelestis
12
0
Hello,

I was wondering if someone could help me with this question to let me know if I'm on the right track?

Homework Statement



We are given two files:
The first is a database file in the format of:
student-id [tab] lab-group [tab] Last-name, Other names
e.g u1234 [tab] tues9-11 [tab] Smith, John
u2345 [tab] wed9-11 [tab] Jones, Fred


The second is a log file in the format of:
student-id [tab] date of submission [tab] filename
e.g u1234 [tab] 15 June 2004 [tab] a2.jar


There is a single tab character between the variables.

It asks us to write a bash script that prints the list of names of students who have *not* submitted any files.

It also needs to take the names of both files as its two command line arguments.



The Attempt at a Solution



This is what i have at the moment:

#!/bin/bash

echo $1,$2

stuID=$(cat log.txt | cut -f1)
echo "$stuID" > submit

stuList=$(cat database.txt | cut -f1)
echo "$stuList" > late

grep -f submit late


Thankyou, I would kindly appreciate your help :)
 
Physics news on Phys.org
  • #2
</code>This looks like it should be a job for a combination of the grep and cut commands. grep can be used to match lines between two files and cut can be used to extract data from specific fields in text.Try something like this:#!/bin/bash#Grab student ID from database file stuID=$(cat $1 | cut -f1)#Grab student ID from log filelogID=$(cat $2 | cut -f1)#Loop through each student ID from the databasefor i in $stuID; do #Check if the student ID is present in the log file if !(grep -q "$i" $2); then #If not present, print the student's name echo $(grep "$i" $1 | cut -f3) fidone
 
  • #3





Hello!

Your attempt at a solution looks like it is on the right track. It seems like you are using the "cut" command to extract specific columns from the files and then using the "grep" command to search for matching lines between the two files. This is a good approach.

One suggestion I have is to add some error handling to your script. For example, if the input files are not provided as command line arguments, your script will not work as intended. You can use the "$#" variable to check the number of arguments and use "if" statements to handle different scenarios. Additionally, you may want to consider using the "awk" command instead of "cut" as it allows for more advanced text processing capabilities.

Overall, I think you are on the right track and with some additional modifications and testing, your script should work as intended. Good luck!
 

What is bash scripting?

Bash scripting is a type of computer programming language used in Unix and Linux operating systems. It allows users to create scripts or programs that automate certain tasks or instructions.

How do I create a bash script?

To create a bash script, you can use any text editor such as Notepad, TextEdit, or Vi. Make sure to save the file with a .sh extension and include a "shebang" at the top of the file that specifies the path to the bash interpreter (#!/bin/bash).

What are variables in bash scripting?

Variables are used to store data or values in a bash script. They can be declared using the syntax: variable_name=value. Variables can contain numbers, strings, or arrays and can be referenced throughout the script.

How do I run a bash script?

To run a bash script, you can use the command "bash scriptname.sh" in the terminal. Make sure the script is executable by using the "chmod +x scriptname.sh" command. You can also run the script by specifying the path to the script, for example: /home/user/scripts/scriptname.sh.

What are some common commands used in bash scripting?

Some common commands used in bash scripting include "echo" for printing messages, "if-else" for conditional statements, "for" and "while" for loops, "grep" for searching text, and "cut" for manipulating data. You can also use various built-in variables and functions in your bash script.

Back
Top