Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
29 views

Unix Exercise

This document provides examples of using Unix/Linux commands and shell scripting. It covers topics such as awk programming, crontab for scheduling jobs, writing shell scripts, shell script arguments, loops, conditions, functions and more.

Uploaded by

K.H. Lee
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views

Unix Exercise

This document provides examples of using Unix/Linux commands and shell scripting. It covers topics such as awk programming, crontab for scheduling jobs, writing shell scripts, shell script arguments, loops, conditions, functions and more.

Uploaded by

K.H. Lee
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Unix Example

[1] awk programming and running


$ cp ../unix300/example.awk .
$ cp ../unix300/data.txt .
$ cat example.awk
BEGIN {
num = 0; a[2] =0 ; a[3] = 0
}
{
print $0
sum=0
for ( i=2 ; i<=NF ; i++ ) {
sum += $i
a[i] += $i
}
printf("%s : sum %d avg is %.2f \n", $1, sum, sum/(NF-1))
num++
}
END {
for ( i=2 ; i<=4 ; i++ ) {
printf "%d th sum %d average %.2f \n", i, a[i], a[i]/NR
}
}
$ cat data.txt
smith 90 80 70
junho 50 90 55
khlee 60 80 70
chulsu 60 60 60
abc 90 30 70
suhan 90 96 99
$ awk -f example.awk data.txt
$ cat daat.txt | awk -f example.awk // same

[2] Periodic job schedule


Log in as a root
$ EDITOR=vi
$ export EDITOR
$ crontab -l
$ crontab -e
40 11 * * 3 /usr/sbin/poweroff
-> save and exit

[3] One time job schedule


$ data
$ tty -> terminal is like /dev/pts/3
$ at 11:30
at> banner Hi | write root pts/3
at> ^d
$ atq

[4] Shell script example #1


$ cat hello.sh
id
pwd
ls -sCF
echo "user is $USER shell is $SHELL"
echo Today is `date`
$ sh hello.sh
$ chmod +x hello.sh
$ hello.sh
$ ./hello.sh
$ echo $PATH
$ PATH=$PATH:.
$ hello.sh

[5] shell script with arguments


$ cat args.sh
echo you are now running program : $0
echo The value of the 1st argument is : $1
echo The value of the 2nd argument is : $2
echo The value of the 3rd argument is : $3
echo The value of the 4th argument is : $4
echo The value of the 5th argument is : $5
echo The number of argument is : $#
echo All argument is : $*
echo 'What is $@ : ' $@
shift
echo after shift
echo The value of the 1st argument is : $1
echo The value of the 2nd argument is : $2
echo The value of the 3rd argument is : $3
$ sh args.sh 1 2 3 4 5

[6] shell script operation


$ cat comp.sh
#!/bin/sh

/usr/ucb/echo -n "Please input two numbers : "


read num1 num2
echo "$num1 + $num2 = \c"
expr $num1 + $num2
echo "$num1 - $num2 = \c"
expr $num1 - $num2
echo "$num1 * $num2 = \c"
expr $num1 \* $num2
echo "$num1 % $num2 = \c"
expr $num1 / $num2

[7] for loop example 1


$ cat for1.sh
for x in 1 2 3 4 5
do
echo "2 * $x is ∖c"
expr $x ∖* 2
done

[8] for loop example 2


$ cat for2.sh
read num
for x in num
do
echo "2 * $x is ∖c"
expr $x ∖* 2
done

[9] for loop example 3


$ cat for3.sh
for x
do
echo "2 * $x is ∖c"
expr $x ∖* 2
done
$ for3.sh 1 3 5 7 9

[10] If condition example 1


$ cat if1.sh
/usr/ucb/echo -n 'enter a number: '
read num
if [ $num -lt 10 ]
then
echo The number is less than 10
fi
echo bye

[11] If condition example 2


$ cat if2.sh
/usr/ucb/echo -n 'enter a number: '
read num
if test $num -gt 10
then
echo The number is greater than 10
else
echo The number is less than 10
fi
echo bye

[11] case example


$ cat case.sh
/usr/ucb/echo -n 'enter a number: '
read num
case $num in
1 | a )
echo The number is greater than 10
pwd
;;
“2” | “b” )
echo The number is less than 10
ls –ld .
;;
* )
echo The number is less than 10
;;
esac
echo bye

[11] function example


$ cat fun1.sh
#!/bin/sh

Repeat () {
echo “I don\’t know $1 $2 \c”
return 7
}
Repeat Your Name
echo $?
exit 3

$ fun1.sh
I don’t know Your Name
7
$ echo $?
3

[12] Number guess example


$ cat numguess.sh
#!/bin/sh

num=`perl -e 'print (sprintf("%.0f",rand(99)) + 1);'`


k=0
echo "Input a number between 0 to 99 : \c"
while [ $num -ne 999 ]
do
read i
if [ $i -eq $num ]
then
echo Congratulations~ You guessed it~
break
else
if [ $i -gt $num ]
then
echo "Input more smaller number : \c"
else
echo "Input more larger number : \c"
fi
k=`expr $k + 1`
fi
done
echo "You dit it $k times"

[10] file test example


$ cat if1.sh
if [ -d $1 ]
then
echo The file $1 is a directory
ls –ld $1
fi
if [ -x $1 ]
then
echo The file $1 cab be executed
ls –ld $1
fi
echo bye

[12] backup procedure


At comunix.snut.ac.kr
$ mkdir mydir
$ echo hi > mydir/hi.txt
$ tar cvf mydir.tar mydir
$ compress mydir.tar
$ ftp 10.10.54.75
Login using id/passwd
ftp> bi
ftp> put mydir.tar.Z
ftp > quit
$ telnet 10.10.54.75
Login using id/passwd
$ uncompress mydir.tar.Z
$ tar xvf mydir.tar

[13] programming with make utility


$ cat main.c
#include "io.h"

void main() {
printf("I'm in main\n");
sub1();
sub2();
printf("I'm in main\n");
}

$ cat sub1.c
#include "io.h"

void sub1() {
printf("I'm in sub1\n");
}

$ cat sub2.c
#include "io.h"

void sub2() {
printf("I'm in sub2\n");
}

$ cat io.h
#include <stdio.h>
void sub1();
void sub2();

$ cat makefile
maketest : main.o sub1.o sub2.o
cc -o maketest main.o sub1.o sub2.o
main.o : main.c io.h
cc -c main.c
sub1.o : sub1.c io.h
cc -c sub1.c
sub2.o : sub2.c io.h
cc -c sub2.c
$ make
$ make -f makefile
$ cc -o maketest main.c sub1.c sub2.c
[14] DOS batch example
c:\tmp> copy con ls.bat
dir %*
^z
c:\> ls
c:\> ls c:\

[15] DOS batch for loop example


c:\tmp> type for1.bat
@echo off
FOR /L %%i IN (1,1,3) DO (
FOR /L %%j IN (1 1 3) DO (
echo i = %%i, j = %%j
)
)
c:\tmp> for1
i = 1, j = 1
i = 1, j = 2
i = 1, j = 3
i = 2, j = 1
i = 2, j = 2
i = 2, j = 3
i = 3, j = 1
i = 3, j = 2
i = 3, j = 3

[16] shell initialization file


$ cd
$ vi .profile
-> add following
EDITOR=vi
export EDITOR
stty erase [Control + V][Back Space Key]
PATH=$PATH:.

You might also like