unix/linux basics 0011

Post on 31-Dec-2015

31 Views

Category:

Documents

1 Downloads

Preview:

Click to see full reader

DESCRIPTION

Unix/Linux basics 0011. Operating systems lab Gergely Windisch windisch.gergely@nik.bmf.hu room 4.12 http://nik.bmf.hu/gwindisch/os_2010. Permissions (quiz) ‏. touch file1 chmod 354 file1 chmod a-X file1 chmod ug+r file1 chmod o-w file1 - PowerPoint PPT Presentation

TRANSCRIPT

Unix/Linux basics0011

Operating systems labGergely Windisch

windisch.gergely@nik.bmf.huroom 4.12

http://nik.bmf.hu/gwindisch/os_2010

Permissions (quiz)

• touch file1• chmod 354 file1• chmod a-X file1• chmod ug+r file1• chmod o-w file1• chmod g-w file1

What is the result?Octal number?Textual representation?

inode (quiz)

• Let's assume that we have two disks. Disk1 and Disk2. Disk1 holds /, Disk2 holds /home.

• Assume that the inode numbers increase by one each and every time a new inode is created. The next free inode number is 345 on Disk1 and 763 on Disk2.

• We run the following commands:cd ~echo "inodes rule" > ~/truthcp truth t2cp truth t3mv t2 t4ln t5 t3ln t4 /etc/t6mv /etc/t6 ~/t7ln /etc/t7 t8

What are the inode numbers for truth, t2, t3, t4, t5, t6, t7 t8?

Control structures if [ logical_expression ]

then...commands...

elif [ logical_expression_2 ]then

...commands...else

...commands...fi

Logical expressions

if [ -r filename ] : switches to analize files-r : see if file exists and readable-w: file exists and writeable... man test

Comparing numbers: -eq instead of == $n1 -eq $n2 $n1 -ne $n2 , gt, ge, lt, le

Logical expressions 2

Comparing strings if [ ”string1” == ”string2” ]

then echo...fi

string1 and 2 could be $variable1 and $variable2

white spacing matters!!! spaces must be places around [, == and ].

Case case ”variable” in

”string1”) commands;;”string2”) commands;;*) commands;;esac

apple=1case ”$apple” in”1”) ... ;;”2”) ... ;;esac

for

for iteration_variable in listdo

commandsdone

cycle steps over the elements of lists list: "one two three four five six seven"

a list of words divided by SPACE can be written as a string or generated by a

command

for (continued)

In BASH scripts we usually use for to iterate over lists of files

use seq to have a "convetional" for loop for i in `seq 30`

do commandsdone

seq 10 returns: 1 2 3 4 5 6 7 8 9 10

Until, while until [ ”$K” -eq ”3” ]; do

commands

done while [ ”$K” -ne ”3” ]; do

commands

done

Input from user

read read K read -n1 K : takes only one character (without

return)

Exercise 1

Create a shell script which takes a number as an input parameter and then writes back in English the „name” of that number. If the given number is not between 0 and 9, it should say: out of bounds (or any other error message you find amusing).

Solution #!/bin/bash

echo ”You have given me: ” case ”$1” in

”1”) echo ”one”;;”2”) echo ”two”;;...*) echo ”too large”

esacexit 0

Exercise 2

Create a shell script that shows a list of options to the user. If the user presses 1, the program should list contents of the current working directory. Pressing two tells the user the name of the current directory, and if the user presses 3, the program should quit. There should be an error message for any other keys.

Solution #!/bin/bash K=0 echo '**********************' echo '* Menu *' echo '* 1 - Pritnt name *' echo '* 2 - Contents *' echo '* 3 - Quit *' echo '**********************' echo Please choose one: read K case "$K" in "1") ls -l

uptime;; "2") pwd;; "3") echo Bye!;; *) echo "Choose one between 1 and 3";; esac

Exercise 3 Create a shell script that takes the name of a file as

an input parameter and a string that should be inserted in that file. The string can be omitted, in that case the program should ask for it from the user. Once it has the name of the file and the string, it should append the string to the end of the file, but only if the said file exists, is a text file and is writeable. If not, give a customized error message. If the program is started without parameters, it should print usage information.

Hints for Exercise 3

first input parameter: $1 second input parameter: $2 number of input parameters: $# asking the user something: read varname man test to find out how to check for

writeable files appending example: echo "$something" >> file

Exercise

Create a program which prints the content of all the files in the current directory. Use for.

Solution

for i in `ls`do

cat $idone

Exercise

Create a shell script that prints out all the input parameters the user has entered in the form of:

parameter 1 is: oneparameter 2 is: two etc.

Exercise Create a user friendly shell script that requires an

input string which should be appended to a file. The file name and the string should come as parameters. If -v is an input parameter, it should do it's job verbosely (tell the user what it is doing at any given point). If -h is given, then don't do anything but return some helpful information (the same effect should come when there are fewer than two parameters).

Exercise

Write a shell script that prints the content of the current directory in the form of:”File: name_of_file”

(bonus points: try to separate directories from files)

Solution

for i in `ls`do

echo ”File: $i”done

Exercise

Create a shell script that copies the contents of all the ASCII text files into one big file.

Hint

use the file command to get the type of the file

Solution #!/bin/bash

allthetext="newfile"echo `date` > $allthetextfor i in `ls`do something=`file $i` something2="$i: ASCII text" if [ "$something" == "$something2" ] then if [ "$allthetext" != "$i" ] then# echo "$i is a text file" cat $i >> $allthetext fi fidone

Exercise

Add commands to the program with the menu, so that it really wouldn't quit unless the user presses 3

Solution#!/bin/bash

K=0

until [ "$K" -eq "3" ]; do

echo '**********************'

echo '* Menu *'

echo '* 1 - List *'

echo '* 2 - Current dir *'

echo '* 3 - Quit *'

echo '**********************'

echo Please choose one:

read K

case "$K" in

"1") ls -l

uptime;;

"2" pwd;;

"3") echo Bye!;;

*) echo "Choose between 1 and 3!";;

esac

done

User management

username passwords UserID GroupID

User management

• /etc/passwd : user information (clear text)• /etc/shadow : passwords (encoded)• /etc/group : groups– take a look at these

• groupadd : create groups• useradd: create users• /home/username: home of the new users– May not get created by default

• /etc/default/useradd• /etc/skel - default directory structure• Creating a user in linux can be done by modifying a few files

Creating groups

• groupadd gname• groupadd -g – add user definied GID (group ID) - usually above

1000

Creating users

• useradd user1– create user (without settings)

• useradd -D : print the defaults• useradd -g group1 user3

– primary group• useradd -g group1 -G gr2,gr3,gr4 user4

– secondary groups• useradd -m user5

– create home directory automatically• useradd -p password user6

– create password

Creating users - hardcore way

• add a new line to /etc/passwd• create the home directory for the new user• use command passwd username to create a password• Try the new user!

Additional commands

• passwd– modify our password

• passwd usernev– modify the password of an other user

• passwd root - way to "create" root in ubuntu

• groupdel• userdel

Exercise

• Create two new users. One using commands, the other by modifying the passwd file

Change ownership

• files have owners• chown user.group file1– give your file to someone else

• try it with new new users– use alt+f2 - f6 to switch between the consoles

Compression

• tar (tape archiever)• tar -cvzf nameoffile.tar.gz *– pack and compress everything in cwd

• tar -xvzf nameoffile.tar.gz– unpack the contents of nameoffile.tar.gz

• Switches– man tar– x: eXtract, c: create, v: verbose, z: gzip, f: filename

Exercise 6.5

• Use tar to compress all your shell scripts into one file

Solution to Exercise 6.5

tar -cvzf myShellScripts.tar.gz *

Exercise 7

• Create a shell script which takes two numbers as input parameters and add them together (those of you who already know ifs should write a full-featured calculator)

Solution to Exercise 7

#!/bin/bashsum=`expr $1 + $2`echo "The sum of $1 and $2 is $sum"

Exercise 8

• Write a shell script that adds the current date (date command) and the current uptime (uptime command) to the ~/uplog file. There should be a separator after these two data, so that the next run can be identified easily.

Solution to Exercise 8

#!/bin/bashnewfile="~/uptime"date >> $newfileuptime >>$newfileecho "----------------" >>$newfileecho " " >>$newfile

Exercise 9

• Write a shell script that takes two input parameters from the user, and then creates a symbolic link pointing to the file denoted by the first parameter with the name provided as the second parameter.

Solution to Exercise 9

#!/bin/bashln -s $1 $2

Exercise 10

• Write a shell script that prints the contents of the PATH (the one which holds the names of the directories where the shell would look for an executable) variable to a file. The filename is provided in the first input parameter.

Solution to Exercise 10

#!/bin/bashecho "Contents of \$PATH: $PATH" > $1

Exercise 11

• Write a shell script that takes the name of a directory as an input parameter and adds that directory to the PATH variable. Be careful not to overwrite the original content, just add it (and use the standard separator of that variable).

• Advanced versions (require for and if)– check if the directory exists– if the user provides more than one dir, add them as

well

Solution to Exercise 11

#!/bin/bashPATH="$PATH:$1"

Further exercises

• http://nik.uni-obuda.hu/gwindisch/os_2010/4_1.txt• http://nik.uni-obuda.hu/gwindisch/os_2010/4_2.txt• http://nik.uni-obuda.hu/gwindisch/os_2010/4_3.txt

Upload your precious work

• If you want, you can send your work to me. If the solutions are correct (or interesting), it could count in your final grades.

• Send it using the commands:– sudo dhclient eth2 (password: nik119)– scp name_of_tar hallgato@10.4.1.6:~/

• (password: hallgato)

• Or via email

top related