Showing posts with label OS. Show all posts
Showing posts with label OS. Show all posts

WA Shell script part 2

1. WA Shell script to find Fibonacci series of a given number.

echo "enter number"
read n
k=1
i=0
j=1
echo $i
while [ $k -le $n  ]
do
      echo $k
      k=`expr $i + $j`
      i=$j
      j=$k
done

2. WA Shell script to find Fibonacci series of a given number.

echo "Enter Number"
read n
sd=0
no=$n

while [ $n -gt 0  ]
do
      sd=`expr $n % 10`
      n=`expr $n / 10`
      s=$s$sd
done

if [ $no -eq $s ]
then
      echo "pelidrom"
else
      echo "not pelindrom"
fi

2. WA Shell script to find palindrome of a given number.

echo "Enter Number"
read n
sd=0
no=$n

while [ $n -gt 0  ]
do
      sd=`expr $n % 10`
      n=`expr $n / 10`
      s=$s$sd
done

if [ $no -eq $s ]
then
      echo "pelidrom"
else
      echo "not pelindrom"
fi


3. WA Shell script to find Amstrong number or not.

echo "Enter No"
read n
l=`expr length $n`
i=1
while [ $i -le $l ]
do
      r=`expr $n | cut -c $i`
      let i++
      m=`expr $r \* $r \* $r`
      s=`expr $s + $m`
     
done

if [ $s -eq $n  ]
then
echo "No. is Amstrong"
else
echo "No. is not Amstrong"

fi

WA Shell script example

WA Shell script to find given number is odd or even.

echo "Enter No"
read a
e=`expr $a % 2`
if test $e -eq 0
then
echo "Even"
else
echo "odd"
fi


2. WA Shell script to find given number is prime or not.

echo "enter no"
read n
i=1
k=0
while [ $i -lt $n  ]
do
      j=`expr $n % $i`
      if [ $j -eq 0  ]
      then
            k=`expr $k + $i` 
      fi
      i=`expr $i + 1`
done

if [ $k -eq $n  ]
then
      echo "No. is prime"
else
      echo "No. is not prime"
fi

3. WA Shell script to find factorial of a given number.

echo "Enter the No"
read a
fact=1
no=$a

while [ $no -ge 1 ]
do
      fact=`expr $fact \* $no`
      let no--
done
echo "factorial of $a is $fact"

OS command

  1. $date
Date command is used to display date date and time

-$date +%m
Displays only month
-$date +%h
Displays only month in word
-$date +%d
Displays only date
-$date +%y
Displays only year
-$date +%H/M/S
Displays Hours/Minutes/Seconds

  1. $man
Displays manual for commands
  1. $cal
Displays the current month calendar
  1. $who
Displays the List of users currently logged in
  1. $who am i
Displays the user’s own logged in details
  1. $echo
Displays given arguments
  1. $bc
Provide calculator facilities
  1. $passwd
Used to change current password of the user
  1. $uname
Displays configuration of the system
-r Displays the version of the OS
-s Displays the name of the OS

  1.  $pwd
Stands for Present Working Directory and displays know current working directory.
      11. mkdir - make directories
mkdir [OPTION] DIRECTORY
Options
Create the DIRECTORY(ies), if they do not already exist.

 Mandatory arguments to long options are mandatory for short options too.
 -m, mode=MODE  set permission mode (as in chmod), not rwxrwxrwx - umask
 -p, parents  no error if existing, make parent directories as needed
 -v, verbose  print a message for each created directory
 -help display this help and exit
 -version output version information and exit

cd - change directories
Use cd to change directories. Type cd followed by the name of a directory to access that directory.Keep in mind that you are always in a directory and can navigate to directories hierarchically above or below.

mv- change the name of a directory
Type mv followed by the current name of a directory and the new name of the directory.
 Ex: mv testdir newnamedir

pwd - print working directory
will show you the full path to the directory you are currently in. This is very handy to use, especially when performing some of the other commands on this page

 rmdir - Remove an existing directory
 rm -r

chmod - change file access permissions
Usage
chmod [-r] permissions filenames
 r  Change the permission on files that are in the subdirectories of the directory that you are currently in.        permission  Specifies the rights that are being granted. Below is the different rights that you can grant in an alpha  numeric format.filenames  File or directory that you are associating the rights with Permissions
u - User who owns the file.
g - Group that owns the file.
o - Other.
a - All.
r - Read the file.
w - Write or edit the file.
x - Execute or run the file as a program.
Numeric Permissions:
CHMOD can also to attributed by using Numeric Permissions:
400 read by owner
040 read by group
004 read by anybody (other)
200 write by owner
020 write by group
002 write by anybody
100 execute by owner
010 execute by group
001 execute by anybody

ls - Short listing of directory contents
-a        list hidden files
-d        list the name of the current directory
-F        show directories with a trailing '/'
            executable files with a trailing '*'
-g        show group ownership of file in long listing
-i        print the inode number of each file
-l        long listing giving details about files  and directories
-R        list all subdirectories encountered
-t        sort by time modified instead of name

cp - Copy files
cp  myfile yourfile
Copy the files "myfile" to the file "yourfile" in the current working directory. This command will create the file "yourfile" if it doesn't exist. It will normally overwrite it without warning if it exists.
cp -i myfile yourfile
With the "-i" option, if the file "yourfile" exists, you will be prompted before it is overwritten.
cp -i /data/myfile
Copy the file "/data/myfile" to the current working directory and name it "myfile". Prompt before overwriting the  file.
cp -dpr srcdir destdir
Copy all files from the directory "srcdir" to the directory "destdir" preserving links (-poption), file attributes (-p option), and copy recursively (-r option). With these options, a directory and all it contents can be copied to another dir

ln - Creates a symbolic link to a file.
ln -s test symlink
Creates a symbolic link named symlink that points to the file test Typing "ls -i test symlink" will show the two files are different with different inodes. Typing "ls -l test symlink" will show that symlink points to the file test.

locate - A fast database driven file locator.
more - Allows file contents or piped output to be sent to the screen one page at a time
cat - Sends file contents to standard output. This is a way to list the contents of short files to the screen. It works well with piping.
wc - Print byte, word, and line counts
cat files - Prints the contents of the specified files.
cmp file1 file2 - Compares two files, reporting all discrepancies. Similar to the diff command, though the output format differs.
diff file1 file2 - Compares two files, reporting all discrepancies. Similar to the cmp command, though the output format differs.

$comm – Displays output in three columns 1st column contains lines unique to the 1st file, 2nd column contains lines unique to the 2nd file and 3rd column displays lines common in both files.
$cut - Used to extract specific columns from the particular file.
$cut –c [COLUMNS-COLUMNS] file
$paste - Used to combine 2 files output vertically
$uniq – Require sorted file as an input and displays one copy of each line
          -u select only those lines that are not repeated
          -d displays only one copy of repeated line
          -c displays frequency of occurrence of all lines
$head – Displays the starting ten lines of the file
          -n displays top n lines
$tail – Displays the ending ten lines of the file
          -n displays n bottom lines
$tr – Translate character in line with specific character
          -s compressing multiple sequential characters
          -c complementing values expression
$grep – Searches for particular pattern in files
          -i ignoring case
          -n displays line number that contains pattern
          -c displays only number of lines that contains pattern
           -l displays only name of the files containing pattern

          -v returns line that do not match with pattern

Introduction to UNIX operating system, Linux Operating System and Open source.


UNIX is an interactive system designed to handle multiple processes and multiple users at the same time. It was designed by programmers, for programmers, to use in an environment in which the majority of the users are relatively sophisticated and are engaged in (often quite complex) software development projects. In many cases, a large number of programmers are actively cooperating to produce a single system, so UNIX has extensive facilities to allow people to work together and share information in controlled ways. The model of a group of experienced programmers working together closely to produce advanced software is obviously very different from the personal computer model of a single beginner working alone with a word processor, and this difference is reflected throughout UNIX from start to finish.

What is it that good programmers want in a system? To start with, most like their systems to be simple, elegant, and consistent. For example, at the lowest level, a file should just be a collection of bytes. Having different classes of files for sequential access, random access, keyed access, remote access, etc. (as mainframes do) just gets in the way. Similarly, if the command

ls A*
means list all the files beginning with “A” then the command
rm A*
should mean remove all the files beginning with “A” and not remove the one file whose name consists of an “A” and an asterisk. This characteristic is sometimes called the principle of least surprise.

Interfaces to UNIX

A UNIX system can be regarded as a kind of pyramid, as illustrated in Fig. 10-1. At the bottom is the hardware, consisting of the CPU, memory, disks, terminals, and other devices. Running on the bare hardware is the UNIX operating system. Its function is to control the hardware and provide a system call interface to all the programs. These system calls allow user programs to create and manage processes, files, and other resources.

Programs make system calls by putting the arguments in registers (or sometimes, on the stack), and issuing trap instructions to switch from user mode to kernel mode to start up UNIX, Since there is no way to write a trap instruction in C, a library is provided, with one procedure per system call. These procedures are written in assembly language, but can be called from C. Each one first puts its arguments in the proper place, then executes the trap instruction. Thus to execute the read system call, a C program can call the read library procedure. As an aside, it is the library interface, and not the system call interface, that is specified by POSIX. In other words, POSIX tells which library procedures a conformant system must supply, what their parameters are, what they must do, and what results they must return. It does not even mention the actual system calls.
In addition to the operating system and system call library, all versions of UNIX supply a large number of standard programs, some of which are specified by the POSIX 1003.2 standard, and some of which differ between UNIX versions. These include the command processor (shell), compilers, editors, text processing programs, and file manipulation utilities. It is these programs that a user at a terminal invokes.

Thus we can speak of three different interfaces to UNIX: the true system call interface, the library interface, and the interface formed by the set of standard utility programs. While the latter is what the casual user thinks of as “UNIX,” in fact, it has almost nothing to do with the operating system itself and can easily be replaced.

Some versions of UNIX, for example, have replaced this keyboard-oriented user interface with a mouse-oriented graphical user interface, without changing the operating system itself at all. It is precisely this flexibility that makes UNIX so popular and has allowed it to survive numerous changes in the underlying technology so well.

 

The UNIX Shell

Many UNIX systems have a graphical user interface of the kind made popular by the Macintosh and later Windows. However, real programmers still prefer a command line interface, called the shell. It is much faster to use, more powerful, easily extensible, and does not give the user RSI from having to use a mouse all the time. Below we will briefly describe the Bourne shell (sh). Since then, many new shells have been written (ksh, bash, etc.). Although UNIX fully supports a graphical environment (X Windows), even in this world many programmers simply make multiple console windows and act as if they have half a dozen ASCII terminals each running the shell.

When the shell starts up, it initializes itself, then types a prompt character, often a percent or dollar sign, on the screen and waits for the user to type a command line.

When the user types a command line, the shell extracts the first word from it, assumes it is the name of a program to be run, searches for this program, and if it finds it runs the program. The shell then suspends itself until the program terminates, at which time it tries to read the next command. What is important here is simply the observation that the shell is an ordinary user program. All it needs is the ability to read from and write to the terminal, and the power to execute other programs

It is possible to put a list of shell commands in a file and then start a shell with this file as standard input. The (second) shell just processes them in order, the same as it would with commands typed on the keyboard. Files containing shell commands are called shell scripts. Shell scripts may assign values to shell variables and then read them later. They may also have parameters, and use if, for, while, and case constructs. Thus a shell script is really a program written in shell language. The Berkeley C shell is an alternative shell that has been designed to make shell scripts (and the command language in general) look like C programs in many respects. Since the shell is just another user program, various other people have written and distributed a variety of other shells.

10.2.5 Kernel Structure

In Fig. 10-1 we saw the overall structure of a UNIX system. Now let us zoom in and look more closely at the kernel before examining the various parts. Showing the kernel structure is slightly tricky since there are many different versions of UNIX, but although the diagram of Fig. 10-3 describes 4.4BSD, it also applies to many other versions with perhaps small changes here and there.

The bottom layer of the kernel consists of the device drivers plus process dispatching. All UNIX drivers are classified as either character device drivers or block device drivers, with the main difference that seeks are allowed on block devices and not on character devices. Technically, network devices are character devices, but they are handled so differently that it is probably clearer to separate them, as has been done in the figure. Process dispatching occurs when an interrupt happens. The low-level code here stops the running process, saves its state in the kernel process table, and starts the appropriate driver. Process dispatching also happens when the kernel is finished and it is time to start up a user process again. Dispatching code is in assembler and is quite distinct from scheduling.

Above the bottom level, the code is different in each of the four “columns” of Fig. 10-3. At the left, we have the character devices. There are two ways they are used. Some programs, such as visual editors like vi and emacs, want every key stroke as it is hit. Raw terminal (tty) I/O makes this possible. Other software, such as the shell (sh), is line oriented and allows users to edit the current line before hitting ENTER to send it to the program. This software uses cooked mode and line disciplines.

Networking software is often modular, with different devices and protocols supported. The layer above the network drivers handles a kind of routing function, making sure the right packet goes to the right device or protocol handler. Most UNIX systems contain the full functionality of an Internet router within the kernel, although the performance is less than that of a hardware router, but this code predated modern hardware routers. Above the router code is the actual protocol stack, always including IP and TCP, but sometimes additional protocols as well. Overlaying all the network is the socket interface, which allows programs to create sockets for particular networks and protocols, getting back a file descriptor for each socket to use later.

On top of the disk drivers are the file system’s buffer cache and the page cache. In early UNIX systems, the buffer cache was a fixed chunk of memory, with the rest of memory for user pages. In many modern UNIX systems, there is no longer a fixed boundary, and any page of memory can be grabbed for either function, depending on what is needed more.

On top of the buffer cache come the file systems. Most UNIX systems support multiple file systems, including the Berkeley fast file system, log-structured file system, and various System V file systems. All of these file systems share the same buffer cache. On top of the file systems come file naming, directory management, hard link and symbolic link management, and other file system properties that are the same for all file systems.

On top of the page cache is the virtual memory system. All the paging logic is here, such as the page replacement algorithm. On top of it is the code for mapping files onto virtual memory and the high-level page fault management code. This is the code that figures out what to do when a page fault occurs. It first checks if the memory reference is valid, and if so, where the needed page is located and how it can be obtained.

The last column deals with process management. Above the dispatcher is the process scheduler, which chooses which process to run next. If threads are managed in the kernel, thread management is also here, although threads are managed in user space on some UNIX systems. Above the scheduler comes the code for processing signals and sending them to the correct destination, as well as the process creation and termination code.


The top layer is the interface into the system. On the left is the system call interface. All system calls come here and are directed to one of the lower modules, depending on the nature of the call. On right part of the top layer is the entrance for traps and interrupts, including signals, page faults, processor exceptions of all kinds, and I/O interrupts.