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.

How far have you got with each?

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.

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

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

Good.

Yes, this test fails because str1 has a trailing space, ie, the string is five characters long, not four, so does not match.

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.

1 Like

The leading "$ " is the prompt, so you don't type that.

Have you covered variables and how they work and how they are expanded by the shell?

You can do alot of playing around with variables purely with "echo"

FOO=foo
BAR=bar
echo $FOO
echo $BAR
echo $FOO $BAR
echo $FOO                         $BAR
echo "$FOO                        $BAR"

and some more

FLIM="flim         "
FLAM="        flam"
echo $FLIM
echo $FLAM
echo "$FLIM"
echo "$FLAM"
echo $FLIM $FLAM
echo "$FLIM $FLAM"

yes i have been through variables.

enter after the leading $

str1=abcd
test $str1 = abcd

then what can i do after this to get my exit result of 0.

$? is a variable that holds the return code of the last command.

echo $?

also

$$ is a variable that holds the current process id
$! is a variable that holds the pid of a process launched asynchronously
$0 holds the current script name
$1 ..... hold each argument from the command line
$@ holds all the command line arguments

Thanks prorter i got it now. :smiley:

Any chance you being able to do any of the above scripts and then going through them with me.

I suggest you take the first one and have a go.

Start the file with

#!/bin/sh -x

call it something sensible and then do

chmod +x myscript.sh

so that the operating system know it can run it.

The -x option will make the shell spit out the detail of what it is really doing.

Iv created a file called filex and i have inserted the env details to it.

Did this by touch filex
then env > filex
checked to see if file has got the correct content in by cat filex.

Then iv created a script by creating a file - touch firstline
and the opening it in vi editor.

Does my first line not have to #!/bin/bash or what u have stated.

get stuck here...

1 Like

I suggest you stick to "sh", not "bash", "zsh", "ksh", "csh" to "tcsh".

You're trying to learn the basics.

Oh its just that my material is telling me to use bin/bash and all of the examples given use this too.

If the documentation is telling you to use bash, then do so.

bash is a superset of sh.

sh is installed on *all* UNIX boxes, bash is only on some. So if you use sh and limit yourself to features sh supports then your script can run on more platforms.

Cool. Ryte to make it easy for myself iv changed the content of my filex to just 10 line of plain and simple text. Now from what i understand the script (firstline) should take the first argument and insert it into the very top (the first line) of the file named in your second argument. The file must reatin its original name too. Any Ideas...

sorry correction - I have gotta rite 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.

Have you been introduced to "sed" ?

#!/bin/sh

echo "$1" | cat - "$2" >$$
mv $$ "$2"