A little help please


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting A little help please
# 1  
Old 11-14-2007
A little help please

Hello everyone im new to this forum and to UNIX itself. Iv only been using using UNIX for about 2 weeks now and have kind of come to a dead end with a problem that im suffering from. Im doing a training course from home and im stuck on the exercises on the end of the chapter. I am using the Putty client on a unix server (SSH).

The exercises are as follows:

SCRIPT 1

Pre-requisites
Create a file with x amount of lines in it, the content of your choice.

Write a script named 'firstline' that takes two arguments. The first being a line of text, the second being your newly created file. The script should take the first argument and insert it into the very top (the first line) of the file named in your second argument.

Note! The file must retain the original name

SCRIPT 2

Take a copy (backup) and amend the above script so that it now inserts the first argument into the middle of the file. Please note that no matter how many lines there are in your script it should always place the line of text in the middle. Name this script 'middleline'.

Note! Again the original file must retain it original name

Note!
The solution to the following problems can be achieved in anything from 10 to 25 lines of code, depending on your aproach. If you have used more than this then you are probably on the wrong track - think again!
Again, for the following and any subsequent exercises you ARE NOT permitted to use either SED or AWK.

SCRIPT 3

Run the command's below
env >> xx
env >> xx
env >> xx
env >> xx
env >> xx

You will now have a file called XX with the env redirected into it 5 times

Create a script named 'username' that takes 1 argument being a file, in this instance we will use the newly created file above xx. Read the input file and look for occurrences of the current user who is executing the script. On finding an occurrence of the username take that line and append it to a file and display a line number and a bracket against the saved line.

The newly created file should look similar to this:
1) PWD=/home/lee.ballancore
2) USER=lee.ballancore
3) MAIL=/var/spool/mail/lee.ballancore
4) LOGNAME=lee.ballancore
5) HOME=/home/lee.ballancore
6) PWD=/home/lee.ballancore
7) USER=lee.ballancore
8) MAIL=/var/spool/mail/lee.ballancore
9) LOGNAME=lee.ballancore
10) HOME=/home/lee.ballancore

Note! The use of a temporary file is not allowed

SCRIPT 4

Write a script named 'permissions' that takes a file as an argument. The script should be able to determine what permissions the owner, group and everybody has for the file passed in. The output should be displayed similar to this.

READ WRITE EXECUTE
OWNER LEE.BALLANCORE YES YES NO
GROUP USERS YES NO NO
EVERYBODY NO NO NO

SCRIPT 5

Create a script named 'userprocesses' which will allow you to find all users currently logged on to the system and display the processes they are running. The heading for each user must be their real name not their log in name.
I.e. my login name is lee.ballancore but my real name is Lee Ballancore
The users must not be displayed more than once.
The output should look similar to this.


Lee Ballancore
PID TTY TIME CMD
31799 pts/3 00:00:00 vim
31866 pts/3 00:00:00 vim
2495 pts/7 00:00:00 vim
8368 pts/0 00:00:00 vim
9544 pts/2 00:00:00 ps

Alistairr Rutherford
PID TTY TIME CMD
8368 pts/0 00:00:00 vim
9544 pts/2 00:00:00 ps


Write the output to a file.


Once complete, save the five scripts in your $HOME/coursework/chapter7 folder.

If you can post the syntax for any of these scripts it would me very much appreciated. Many thanks in advance.
# 2  
Old 11-14-2007
How far have you got with each?
# 3  
Old 11-14-2007
Iv been trying to go through the relvant material to help me to complete these exersises. But after doing the If/Decisions statements and commands teh stuff has just seemed to have cabbaged my head. So in fact i really dont knw where to begin here.
# 4  
Old 11-14-2007
I suggest

(a) start the book again

(b) type in the examples from the book

(c) step through each example and understand what it is doing and how it works

(d) take each example and try and change it some how to deliberately change it's behaviour, do the changes you make have the expected result?

(e) make up your own scripts based on the commands used in the examples upto that point
# 5  
Old 11-14-2007
Thing is i understand evrything to that point but then when i get onto stuff liek testing with conditions - testing charachter data, testing numeric data and testing for files. Let me give you an example if you explain this little part would be great.

Although test is most often used for decision making, it can also be used on its own as follows

$ str1=abcd
$ test $str1 = abcd
$ echo $?
0


Note: unlike the variable assignment statement in the first line in the preceding example, the test command must have the equal sign surrounded by white space.

In this example, the shell sends three arguments to test. Strings must be equivalent in both length and character by character.

$ str1="abcd "
$ test "$str1" = abcd
$ echo
$?
1
# 6  
Old 11-14-2007
Quote:
Originally Posted by immyakram
$ str1=abcd
$ test $str1 = abcd
$ echo $?
0
Good.

Quote:
Originally Posted by immyakram
$ str1="abcd "
$ test "$str1" = abcd
$ echo $?
1
Yes, this test fails because str1 has a trailing space, ie, the string is five characters long, not four, so does not match.

Last edited by porter; 11-14-2007 at 05:26 PM..
# 7  
Old 11-14-2007
i cant seem to run this syntax on putty, any ideas. Im trying to replicate the syntax as shown above. Is there something i am mising.
This User Gave Thanks to immyakram For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread
Login or Register to Ask a Question