Linux Help (1 Viewer)

Emma

Senior Member
Mar 4, 2004
3,753
#1
Just when I thought I wouldnt need to worry about knowing anything about Linux they schedule an 'operating systems' class that hits me with a god damn linux assignment. Its not all doodles and zombies :D

We had like 2 labs where we learnt basic commands etc. Now they expect me to know how to do this!!!

So like start helping :D


--------------------------------------------------------------------------
A1.
Write an executable shell script that reads a specified number of lines <nlines> from a file <source_file>, starting from a given line number <start_line> and writes them to a file <destination_file>

A2.
The parameters <source_file>, <destination_file>, <start_line> and <nlines> are to be specified on the command line.

A3.
Your script should handle the following:
If <destination_file> does not exist, the process must create it.
If <destination_file> already exists, a warning message should be generated with the options to:
continue and override the existing file
continue and append to the existing file

A4.
You should also make clear how are you going to handle the following situations:
<source_file> does not exist
<start_line> is above the total number or lines in <source_file>
<nlines> exceeds the number of lines in <source_file>

Deliverables:
1. A commented shell script, describing what each line does - and making clear how the questions in A4 are addressed.
2. An executable script.
 

gray

Senior Member
Moderator
Apr 22, 2003
30,133
#2
:LOL: I was about to take out a ruler to make sure I had my eyes aligned on the "Thread Name" and "Thread Starter" columns properly
 

gray

Senior Member
Moderator
Apr 22, 2003
30,133
#4
I haven't had enough time on my hands to be comfortable with Linux, but I'm sure Martin could do this in 2 minutes with his eyes closed and his hands tied behind his back... without a computer
 

Martin

Senior Member
Dec 31, 2000
56,866
#5
I have no idea, never bothered to learn this. My shell scripts are just batch scripts with commands that execute sequentially.

You're on your own. :D
 
OP
OP

Emma

Senior Member
Mar 4, 2004
3,753
#6
you guys suck and linux sucks :D

Any useful websites or anything at all? If not im just going to have to cheat again :D
 

Martin

Senior Member
Dec 31, 2000
56,866
#7
erm, dont you have any educational material on that? they don't expect you to do this from memory, do you?

google for bash scripting tutorial, something like that
 
OP
OP

Emma

Senior Member
Mar 4, 2004
3,753
#8
i have two tutorials which i did. but they just give me various commands and how to create file, update file etc. i think im supposed to be able to take it from there and do this :D

im not interested in such things so there is no way im ever going to do it, im to lazy for that. I'll just use my convincing powers to get someone to give me a copy hehe :p

You guys do still suck though, what kinda geeks are you? :D
 
OP
OP

Emma

Senior Member
Mar 4, 2004
3,753
#12
Da da da, who's bad?
Code:
#!/bin/bash

while [ $1 ]; do
case $1 in
        -f)
                source_file=$2
                ;;
        -d)
                destination_file=$2
                ;;
        -s)
                start_line=$2
                ;;
        -n)
                nlines=$2
                ;;
esac
shift
done

if [ "$nlines" = "" -o "$start_line" = "" -o "$source_file" = "" -o "$destination_file" = "" ]; then
        echo "Parameter missing."
        exit 1
fi

if [ ! -f $source_file ]; then
        echo "Could not open source file."
        exit 1
fi

if [ -f $destination_file ]; then
        echo "Destination file already exist."
        while [ "$todo" = "" ]; do
                echo "Do wish to [o]verwrite or [a]ppend data to file? [o/a] "
                read todo
                if [ "$todo" = "o" ]; then
                        rm -f $destination_file
                elif [ "$todo" = "a" ]; then
                        true
                else
                        todo=""
                 fi
        done
fi

typeset -i tlines=$(cat $source_file | wc -l)
typeset -i end_line=$(($start_line + $nlines - 1))

if [ $end_line -gt $tlines ]; then
        echo "Not enough lines in source file."
        exit 1
fi

head -n$end_line $source_file | tail -n$nlines >> $destination_file

exit 0
 
OP
OP

Emma

Senior Member
Mar 4, 2004
3,753
#17
bah I did write it (well made it up from other programs. But still, the credit is mine :D

And with that I will never ever ever do this crap again. Stupid linux.
 
OP
OP

Emma

Senior Member
Mar 4, 2004
3,753
#18
just kidding, totally ripped this off someone. why do it when you can get someone else to? :Dhehe
 

Users Who Are Viewing This Thread (Users: 0, Guests: 1)