I'm new in bash scripting and need help with the following please:

The purpose of this homework is to let students write programs in bash scripting language that enable them to manipulate data in a text file similarly to a database table..

Students should understand the table structure and write the programs check_table, row_select and row_delete using bash scripting language.

Table structure A table consists of a text file that contains lines. The first line contains information about the table and each of the rest of lines represents a data row.

The first line structure is as follows:

table_name:number_of_rows:number_of_fields:fieldname1,fieldname2,filedname3 ...etc
  • table_name is an alphanumeric string not exceeding 16 characters and does not include the column character ‘:’ nor the space character ‘ ‘. It must be the same as the file name.
  • number_of_rows is a integer with value less than 1000.
  • number_of_fields is a integer with value less than 10.
  • filedname is an alphanumeric string not exceeding 16 characters and not including the space ‘ ‘ nor the comma ‘,’ characters.

Each data row consists of a series of fields delimited by the comma character’,’. Each row has exactly the same number of fields as all other fields in the table. A field consists of a string of alphanumeric characters, does not exceed 16 characters and does not contain the space character ‘ ‘ nor the comma character ‘,’.


students_info:3:5:std_1st_name,std_last_name,year_ of_birth,sex,program



syntax: check_table <table_name>


The program checks the following in order:

  • command syntax
  • table exists.
  • first line syntax is correct.
  • tablename and fieldnames all have correct lengths.
  • Table has correct number of rows.
  • All rows have correct number of fields.


  • table is good
  • table has a a problem problem description (first problem encountered)


$ check_table students_info
table students_info is good
$ check_table student_info1
table students_info has at least one problem:
Actual number of rows not equal to the one in the file header.

I need some guide about this.

Thanks in advance.

  • Thanks for the reply, I couldn't figure how to start ? Can you guide me on this please ? – user73778 Jun 28 '12 at 12:44
  • how to start: write #!/bin/bash -xv at the top of a file. Save it. Then, make the file executatble: $chmod +x fileName You actualy dont need to do that, but it makes like easier. You could do sudo exec ./filename every-time you want to run your program. – j0h Apr 26 '14 at 2:28

You should at least start by learning the basics of bash. I recommend reading http://mywiki.wooledge.org/BashGuide.

After reading the guide, you'll want to look at parsing a text file line-by-line and/or field-by-field, which Bash FAQ 1 explains.

You'll also need to do some string manipulations. Bash FAQ 100 should cover that.


Just to help you get started, save this in a file and set the execute mode then run it in a terminal. Look at the output and compare it to the script. See if you can understand how it relates. Use the reference that have already been suggested to help.


if [[ $# != 1 ]]
    echo usage $0 "<table_to_check>"
    exit 1

    while read line # loop through the lines
    # check syntax of "line"
        #check first line syntax
            echo information row=$line # debug

            # ... your method here. Assign to error if something is wrong...

            firstline=0 # ready for next line/loop which should be data
        #check other lines
            echo data row=$line # debug

            # ... your method here. Assign to error if something is wrong...


        if [[ $error != "" ]] # assign any error in the data as a string message you can then print here and exit
            echo $error 
            exit 2
} < "$1"

exit 0 # no error

To implement the field checks perhaps take a look at cut to break the line up into individual tokens, eg in the information row:

number_of_rows=`echo $line | cut -d':' -f2`

and given the sample, $number_of_rows should contain "3"

So to check this is right you could use a counter that gets incremented on each loop:


When there is no more data $rowcount should equal $number_of_rows so:

    if (( $rowcount != $number_of_rows ))
        error="number of rows doesn't agree"
        # ... etc ...

Something like that.

Other methods are available of course

  • < <(cat $1) is better written < "$1"; no point in first having cat read the file, then the shell. Also, quotes. – geirha Jun 28 '12 at 16:29
  • Some more improvements: Splitting out each field of a line using cut is very inefficient, better use read. IFS=: read -ra fields <<< "$line"; echo "First field is ${fields[0]}, second is ${fields[1]}, ..." (See mywiki.wooledge.org/BashFAQ/001). The $[ ... ] syntax for doing math has been deprecated for ages. It's not even documented anymore. Use rowcount=$(( rowcount + 1 )) or ((rowcount++)). – geirha Jun 28 '12 at 22:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.