UNIX head command not working?

Login or Register for Dates, Times and to Reply

 
Thread Tools Search this Thread
Homework and Emergencies Homework & Coursework Questions UNIX head command not working?
# 1  
UNIX head command not working?

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted!

1. The problem statement, all variables and given/known data:

Write a script that allows the user to print the first n lines or the last n lines of every file in the directory specified by the argument. e.g. lshead -head 2 Documents will print the first two lines of every file in the documents directory.


2. Relevant commands, code, scripts, algorithms:

Head command is used in this command as well as the tail command.

3. The attempts at a solution (include all code and scripts):

Code:
if [ $# -lt 0 ];
then
    echo "A directory name as an argument is expected"
    exit 1
fi

if [ $2 -lt 0 ];
then
        echo "expected a positive integer"
        exit 1
fi

if [ $1 = "-head" ];
then
    head -n $3

elif [ $1 "-tail" ];
then
    tail -n $3
else
         echo "Enter either head or tail"
         exit 1
fi
exit 0

I have run it and it comes up with this error:
head: Documents: invalid number of lines

I don't understand why it isn't working, I have even consulted friends on this and even they can't understand why it isn't working.

4. Complete Name of School (University), City (State), Country, Name of Professor, and Course Number (Link to Course):
University of Huddersfield, Huddersfield, United Kingdom, Dr Gary Allen, CFT2112

Note: Without school/professor/course information, you will be banned if you post here! You must complete the entire template (not just parts of it).
# 2  
What are you typing into unix?

Code:
myscript 1 2 3

or something similar, to start your script and pass parameters.
# 3  
I type in
Code:
lshead -head 2 Documents (or whatever directory I want)

$0 = lshead command
$1 = -head/-tail
$2 = numeric value
$3 = directory
# 4  
Isn't that error message highly meaningful? Look at the positional parameter that you supply to the commands.

---------- Post updated at 18:18 ---------- Previous update was at 18:17 ----------

And, why do you expect a negative parameter count?
# 5  
I just put the $2 command there so that if a negative integer was entered, then it would prompt the user to enter a positive one.
# 6  
And what about if the given directory were empty or contain binaries?
And to say the truth I doubt tail/head work on directories...
# 7  
We haven't been told about what would happen if it was empty or contained binaries.

This is one of the commands I have been set so it must work surely.
Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #987
Difficulty: Medium
The FreeBSD TCP/IP stack is based on the 4.2BSD implementation of TCP/IP.
True or False?

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Head command queries

we have a file as below AREA,COUNTRY,RANK A,MX,1 A,MX,2 A,MX,5 A,MX,8 A,IN,7 A,IN,5 A,IN,21 B,CN,6 B,CN,2 B,CN,8 B,CN,0 we need the TOP 2 RANK records for the combination of Area, Country as below. i know head -2, which gives top 2 records from file but not sure it lists based on... (7 Replies)
Discussion started by: JSKOBS
7 Replies

2. Shell Programming and Scripting

sed Command not working in AIX UNIX

Hi 1st problem -------------- i have this sed command in my unix script which replaces new line and carriage return in a line with the string "&#xA" the script works fine in Linux 3.0.101-0.5, but not in AIX 1 7 , the "s/\r/\&#xA/g" replacement, replaces all the character "r" in the file.... (3 Replies)
Discussion started by: maximus_jack
3 Replies

3. Shell Programming and Scripting

UNIX command in cron not working

the following unix command gives me the number of records avaiable in my application /navs/sys/appl/bin/record list This gives number of rows and colums. I want to generate one text file everyday at 6:10 AM using cron So I added following entery into crontable #Generate record file at... (6 Replies)
Discussion started by: Nakul_sh
6 Replies

4. Shell Programming and Scripting

Diff/head - not sure if this is the right command to use

Hi, I need some advise on whether there is a better way of doing what I am currently planning to do. Perhaps I should be using arrays instead of re-directing output to files? I need to use a tool/program named ADRCI provided by Oracle to remove trace files that it generates. Honestly it is... (1 Reply)
Discussion started by: newbie_01
1 Replies

5. Homework & Coursework Questions

Unix find and head command help

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: I've been stuck on this problem for 2 days now What command would you enter to list the first lines of all text... (11 Replies)
Discussion started by: partieboi37
11 Replies

6. Shell Programming and Scripting

cp command not working in script for non unix file

Piece of code in sh file ------------------------ echo "lalit test bef" which cp cp $l_options $srcdirfile $destdirfile echo "lalit test after" echo "**************************options below" echo "loption $l_options" echo "src $srcdirfile" echo "destinat... (3 Replies)
Discussion started by: lalitpct
3 Replies

7. Shell Programming and Scripting

head command with more than one file

Hi, I have the following problem. I have files with one column of data (let's say file1.dat, file2.dat...file6.dat), and I would like to record the first value of the column of each file into another file (let's name it fileall.dat), which would have the the six values, one in each column. I use to... (4 Replies)
Discussion started by: josegr
4 Replies

8. UNIX for Dummies Questions & Answers

alternative for head command

Hi friends,I am new to unix and this is really a dummy question.but please help me out. How to simulate head command without using head command??? also tail command too,also more command. it is given as a homework to do....please tell me how to do (2 Replies)
Discussion started by: nikhilneela
2 Replies

9. Shell Programming and Scripting

head command

Hi All, How can the head command be used to extract only a particular line. By default head -n filename displays the first n lines. I want only the nth line. I couldn't get it from forum search. Thanks, Sumesh (6 Replies)
Discussion started by: sumesh.abraham
6 Replies

Featured Tech Videos