Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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 ‘,’.

Example:

students_info:3:5:std_1st_name,std_last_name,year_ of_birth,sex,program
amer,salim,1990,m,bit
samira,rami,1988,f,ise
lamia,rida,1990,f,ise

Programs

1.check_table

syntax: check_table <table_name>

Description:

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.

Output:

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

example:

$ 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.

share|improve this question
    
what have you tried? –  Web-E Jun 28 '12 at 10:56
    
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 at 2:28

2 Answers 2

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.

share|improve this answer

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.

#!/bin/bash

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

error=""
firstline=1
{
    while read line # loop through the lines
    do
    # check syntax of "line"
        if(($firstline))
        then
        #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
        else
        #check other lines
            echo data row=$line # debug

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

        fi


        if [[ $error != "" ]] # assign any error in the data as a string message you can then print here and exit
        then
            echo $error 
            exit 2
        fi
    done
} < "$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:

rowcount=$[rowcount+1]

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

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

Something like that.

Other methods are available of course

share|improve this answer
    
< <(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

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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