Need help with a really basic assignment, probably gonna take you 5 mins to do.but it worse 7% to me


 
Thread Tools Search this Thread
Homework and Emergencies Homework & Coursework Questions Need help with a really basic assignment, probably gonna take you 5 mins to do.but it worse 7% to me
# 1  
Old 04-16-2012
Error Need help with a really basic assignment, probably gonna take you 5 mins to do.but it worse 7% to me

1. The problem statement, all variables and given/known data:
I am a international student who is studying in Australia, my mother language is not English, so some of the skills are really hard to understand.
the thing is here:

I have got a assignment with 3 requirements:

1.Display the following:
The user’s username and the absolute path of the login directory;
The calendar of 2012;
The total number of files and subdirectories in your login directory;
Accept an input of a positive number N that is not greater than 45 (using the read command), and display the first N lines of the contents of Rail-Stations.txt.
The long listing of the directory specified by the first command line argument, sorted in reversed alphabetic order.
If the second command line argument is provided, all the above output will be redirected to the file specified by the second argument. If the second argument is not provided, the above output should be displayed on the screen.
2.Find the first letter in alphabet from the input and count the input letters:
Accept the input of a series of lower case alphabetic letters one at a time. The input ends with a 0. Find and display the first letter in alphabetic order and the count of total number of valid input letters. For example, input of d, g, t, w, q, b, h, d, s, 0 should result in b and 9.
Any invalid input character (eg. #, $, 3, A) will cause an error message and be ignored.
3.Exit the program
Before exit, ask user to confirm by entering a “Y” (“y”) or “N” (“n”). If confirmed, then exit, otherwise, return to the menu.


im stuck at the half way of the first requirement that is "display the first N lines of the contents of Rail-Stations.txt." then I can not continue any more. I tried to do the 3rd requirement, but i think its not gonna be right.

Somebody help me with it....im really up set, I tried to learn it by myself, but my bad English made this really hard...the best thing is to give me the code, I will learn from thatSmilie


2. Relevant commands, code, scripts, algorithms:
*really basic command...


3. The attempts at a solution (include all code and scripts):
this is the code i did...maybe its gonna help a bit

Code:
#!/bin/sh
echo "This is your username:" $USER
echo "This is the path of your login directory:" $HOME
cal 2012
echo "The amount of files in your directory are:";(ls ~|wc -l)
read n
if[$n -lt 45];
then
head -n Rail-Stations.txt
fi

echo "Exit?(y/n)"
read e
if[e -eq 'y']&&[e -eq 'Y'];
then exit
if[e -eq 'n']&&[e -eq 'N'];
return
fi
fi

4. Complete Name of School (University), City (State), Country, Name of Professor, and Course Number (Link to Course):

UTS:insearch, Sydney, Australia, James Hu, IWBS001(web systems)

Moderator's Comments:
Mod Comment Code tags for code, please.

Last edited by Corona688; 04-16-2012 at 02:03 PM..
# 2  
Old 04-16-2012
Consider [ ] to be commands, not brackets. if[$n is not the same command as [, the spaces are important.

You've left out lots of spaces everywhere and put in semicolons you don't actually need:

Code:
# wrong
if[$n -lt 45];
# right
if [ "$n" -lt 45 ]

You also forgot to use $e here, used the wrong logic(you want || for 'or', since a string can never be 'y' and 'Y' at the same time). Also, -eq is for integers, for strings you want = .

Code:
if [ "$e" = 'y' ] || [ "$e" = 'Y' ]

But I think that bit would be simpler as a case:

Code:
case "$e" in
[yY]) exit
        ;;
[nN]) return
        ;;
esac


Last edited by Corona688; 04-16-2012 at 02:14 PM..
# 3  
Old 04-16-2012
Also, you need a loop for the menu since 'return' doesn't do what you think it does -- it quits the script just like exit does when used there.

Code:
REPLY="Y"

while [ "$REPLY" = "y" ] || [ "$REPLY" = "Y" ]
do
        ...
done

# 4  
Old 04-16-2012
Quote:
Originally Posted by Corona688
Also, you need a loop for the menu since 'return' doesn't do what you think it does -- it quits the script just like exit does when used there.

Code:
REPLY="Y"

while [ "$REPLY" = "y" ] || [ "$REPLY" = "Y" ]
do
        ...
done

thx for your help, and what about the last 2 requirements in question one that is "The long listing of the directory specified by the first command line argument, sorted in reversed alphabetic order.
If the second command line argument is provided, all the above output will be redirected to the file specified by the second argument. If the second argument is not provided, the above output should be displayed on the screen.
"and the 2nd question? I have no idea about these 2.Smilie

---------- Post updated at 03:33 AM ---------- Previous update was at 03:20 AM ----------

Quote:
Originally Posted by Corona688
Consider [ ] to be commands, not brackets. if[$n is not the same command as [, the spaces are important.

You've left out lots of spaces everywhere and put in semicolons you don't actually need:

Code:
# wrong
if[$n -lt 45];
# right
if [ "$n" -lt 45 ]

You also forgot to use $e here, used the wrong logic(you want || for 'or', since a string can never be 'y' and 'Y' at the same time). Also, -eq is for integers, for strings you want = .

Code:
if [ "$e" = 'y' ] || [ "$e" = 'Y' ]

But I think that bit would be simpler as a case:

Code:
case "$e" in
[yY]) exit
        ;;
[nN]) return
        ;;
esac

it shows "head:illegal line count -- Rail-Stations.txt"Smilie

and when I fix the 3rd part of the program,like this :

Code:
#!/bin/sh
echo "This is your username:" $USER
echo "This is the path of your login directory:" $HOME
cal 2012
echo "The amount of files in your directory are:";(ls ~|wc -l)
read n
if [ "$n" -lt 45 ];
then
head -n Rail-Stations.txt
fi

echo "Exit?(y/n)"
read e
if [ "$e" = 'y' ] || [ "$e" = 'Y' ];
then exit
if [ "$e" =  'n'] || [ "$e" = 'N' ];
return
fi
fi

it shows this :

Code:
Exit?(y/n)
y
./ass4: line 18: syntax error near unexpected token `fi'
./ass4: line 18: `fi'

Moderator's Comments:
Mod Comment code tags for code, please.

Last edited by Corona688; 04-16-2012 at 02:45 PM..
# 5  
Old 04-16-2012
To keep the forums high quality for all users, please take the time to format your posts correctly.

First of all, use Code Tags when you post any code or data samples so others can easily read your code. You can easily do this by highlighting your code and then clicking on the # in the editing menu. (You can also type code tags [code] and [/code] by hand.)



Second, avoid adding color or different fonts and font size to your posts. Selective use of color to highlight a single word or phrase can be useful at times, but using color, in general, makes the forums harder to read, especially bright colors like red.

Third, be careful when you cut-and-paste, edit any odd characters and make sure all links are working property.

Thank You.

The UNIX and Linux Forums
# 6  
Old 04-16-2012
From man head:

Code:
       -n, --lines=[-]K
              print  the first K lines instead of the first 10; with the lead-
              ing `-', print all but the last K lines of each file

So -n expects the number of lines. If you're not giving it a number of lines, don't bother telling head -n.

You're improperly nesting your if's. If you want to do if/else, use elif. Or you could break it into two completely separate if-statements. And you still have the useless ;'s there.

And the 'return' still doesn't do anything like what you think it does. It doesn't jump to the top of the script. To loop, you need a loop.

Code:
if [ "$e" = 'y' ] || [ "$e" = 'Y' ]
then exit
elif [ "$e" =  'n'] || [ "$e" = 'N' ]
return
fi # only one fi

The first argument is $1. So ls -l $1. There's another argument you can give ls to reverse its order, which you can find via man ls.

You can tell how many arguments there are with the $# special variable. Something like if [ "$#" -eq 2 ] to do one thing when you have two arguments, and another when you don't.

You can redirect into a file with >. command > filename
This User Gave Thanks to Corona688 For This Post:
# 7  
Old 04-16-2012
Quote:
Originally Posted by Corona688
From man head:

Code:
       -n, --lines=[-]K
              print  the first K lines instead of the first 10; with the lead-
              ing `-', print all but the last K lines of each file

So -n expects the number of lines. If you're not giving it a number of lines, don't bother telling head -n.

You're improperly nesting your if's. If you want to do if/else, use elif. Or you could break it into two completely separate if-statements. And you still have the useless ;'s there.

And the 'return' still doesn't do anything like what you think it does. It doesn't jump to the top of the script. To loop, you need a loop.

Code:
if [ "$e" = 'y' ] || [ "$e" = 'Y' ]
then exit
elif [ "$e" =  'n'] || [ "$e" = 'N' ]
return
fi # only one fi

The first argument is $1. So ls -l $1. There's another argument you can give ls to reverse its order, which you can find via man ls.

You can tell how many arguments there are with the $# special variable. Something like if [ "$#" -eq 2 ] to do one thing when you have two arguments, and another when you don't.

You can redirect into a file with >. command > filename
so
Code:
ls -l $1

is going to do this step?:
Code:
"The long listing of the directory specified by the first command line argument, sorted in reversed alphabetic order."

I dont know how to use
Code:
>

to redirecte all of this results above:
Code:
echo "This is your username:" $USER
echo "This is the path of your login directory:" $HOME
cal 2012
echo "The amount of files in your directory are:";(ls ~|wc -l)
read n
if [ "$n" -lt 45 ]
then
head -n Rail-Stations.txt
fi

to the 2nd argument, because it is in the program, its not like outside the program. i did
Code:
if [ "$# -eq 2 ]

and then what should I do?

also, what can I do with the 2nd question?:
Code:
Find the first letter in alphabet from the input and count the input letters:
  Accept the input of a series of lower case alphabetic letters one at a time. The input ends with a 0. Find and display the first letter in alphabetic order and the count of total number of valid input letters. For example, input of d, g, t, w, q, b, h, d, s, 0 should result in b and 9.
  Any invalid input character (eg. #, $, 3, A) will cause an error message and be ignored.

Smilie got no idea
Login or Register to Ask a Question

Previous Thread | Next Thread

6 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Grep last 5 mins from logs

Hi, system date format Thu Jun 13 12:55:18 EDT 2019 My log date format 09.148.192.60 - - "GET /akamai/sureroute-test-object.html HTTP/1.1" 404 231 can someone please help me, how to get last 5mins of logs please ? I need the command Please wrap your samples/codes in CODE TAGS,... (3 Replies)
Discussion started by: scazed
3 Replies

2. Shell Programming and Scripting

Need logs 5 mins old

I need 5 mins old logs to be dumped into a a new file. The date formats in the two log files are Can you suggect for both formats ? bash-3.2$ uname -a SunOS myserver 5.10 Generic_150400-26 sun4v sparc sun4v ---------- Post updated 05-04-16 at 12:24 AM ---------- Previous update was... (2 Replies)
Discussion started by: mohtashims
2 Replies

3. Shell Programming and Scripting

How to check if there is a file in the last 10 mins?

I have a script that runs every hour from the crontab (see the settings of the crontab below) (15 mins past the hour) 15 * * * * /home/usr/usr1/ProvAll This script saves two files in the following format now=`/bin/date '+%Y%m%d.%H%M%S'` file1.$now (for example) file1.20140722.031502... (12 Replies)
Discussion started by: knijjar
12 Replies

4. Red Hat

When is RHEL 7 Gonna Release?

When is RHEL 7 Gonna Release and When will the Exam Will be Conducted Any one have idea ? :mad: (2 Replies)
Discussion started by: babinlonston
2 Replies

5. Shell Programming and Scripting

tail for 15 mins

Hi, I want to write a script which will tail a particular file for 15 mins and then sleep for 10 mins and again tail for 15 mins. This cycle will go on for a limited period of time. How can i ensure that tail command will run for 15 mins before calling sleep command Thanks (6 Replies)
Discussion started by: @bhi
6 Replies

6. UNIX for Dummies Questions & Answers

files created within last 10 mins

Any simple 1 liners to check a directory to see if a file was created within the last 10 mins? (5 Replies)
Discussion started by: frustrated1
5 Replies
Login or Register to Ask a Question