Sei sulla pagina 1di 43

Working with Directories

1. Display your current directory. [ca300@Linuxserver ~]$ pwd OUTPUT /home/ca300 2. Change to the /etc directory. [ca300@Linuxserver ~]$ cd /etc 3. Go to the parent directory of the current directory. [ca300@Linuxserver etc]$ cd .. 4. Go to the root directory. [ca300@Linuxserver ~]$ cd / 5. List the contents of the root directory. [ca300@Linuxserver /]$ ls OUTPUT bin error home media net proc selinux sys usr boot etc lib misc opt root share tftpboot var dev file1 lost+found mnt output sbin srv tmp 6. List a long listing of the root directory. [ca300@Linuxserver /]$ ll OUTPUT total 170 drwxr-xr-x 2 root root 4096 Apr 9 16:33 bin drwxr-xr-x 4 root root 1024 Dec 20 16:56 boot drwxr-xr-x 12 root root 3720 Apr 25 08:10 dev

7. Stay where you are, and list the contents of /etc. [ca300@Linuxserver /]$ ls /etc OUTPUT a2ps.cfg a2ps-site.cfg acpi inittab inputrc iproute2 racoon rc rc0.d

8. Stay where you are, and list the contents of /bin and /sbin. [ca300@Linuxserver /]$ ls /bin OUTPUT alsacard alsaunmute arch ed egrep env mailx mkdir mknod sh sleep sort

[ca300@Linuxserver /]$ ls /sbin OUTPUT accton addpart adsl-connect ip6tables-restore ip6tables-save ipmaddr nameif nash netplugd

9. Stay where you are, and list the contents of ~. [ca300@Linuxserver /]$ ls ~ OUTPUT 15 8 9 etcbackup exp f newdir newfile newfolder reverse shellscript srividya

10. List all the files (including hidden files) in your home directory. [ca300@Linuxserver ~]$ ls -al OUTPUT total 600 drwx------ 16 ca271 ca271 4096 Apr 24 10:45 . drwxr-xr-x 59 root root 4096 Apr 23 14:46 .. -rw-rw-r-- 1 ca271 ca271 123 Apr 5 10:34 15 11. List the files in /boot in a human readable format. [ca300@Linuxserver /]$ ls -lh /boot OUTPUT total 12M -rw-r--r-- 1 root root 69K Oct 3 2012 config-2.6.18-308.16.1.el5 -rw-r--r-- 1 root root 69K Feb 22 2012 config-2.6.18-308.el5 drwxr-xr-x 2 root root 1.0K Jan 4 11:36 grub 12. Create a directory testdir in your home directory. [ca300@Linuxserver ~]$ mkdir testdir 13. Change to the /etc directory, stay here and create a directory newdir in your home directory. [ca300@Linuxserver etc]$ mkdir /home/ca271/newdir 14. Create in one command the directories ~/dir1/dir2/dir3 (dir3 is a subdirectory from dir2, and dir2 is a subdirectory from dir1 ). [ca300@Linuxserver ~]$ mkdir -p dir1/dir2/dir3 15. Remove the directory testdir. [ca300@Linuxserver ~]$ rm -r testdir

Working with Files


1. List the files in the /bin directory [ca300@Linuxserver /]$ ls /bin OUTPUT alsacard alsaunmute arch ed egrep env mailx mkdir mknod sh sleep sort

2. Display the type of file of /bin/cat, /etc/passwd and /usr/bin/passwd. [ca300@Linuxserver /]$ file /bin/cat OUTPUT /bin/cat: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), for GNU/Li [ca300@Linuxserver /]$ file /etc/passwd OUTPUT /etc/passwd: ASCII text [ca300@Linuxserver /]$ file /usr/bin/passwd OUTPUT /usr/bin/passwd: setuid ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), for GNU/Linux 2.6.9, dynamically linked (uses shared libs), stripped 3. Create a directory ~/touched and enter it. [ca300@Linuxserver ~]$ mkdir touched [ca300@Linuxserver touched]$ 4. Create the files today.txt and yesterday.txt in touched. touch today.txt yesterday.txt

5. Change the date on yesterday.txt to match yesterday's date. [ca300@Linuxserver touched]$ touch --date=04/24/13 yesterday.txt

6. Copy yesterday.txt to copy.yesterday.txt [ca300@Linuxserver touched]$ cp yesterday.txt copy.yesterday.txt 7. Rename copy.yesterday.txt to kim [ca300@Linuxserver touched]$ mv copy.yesterday.txt kim 8. Create a directory called ~/testbackup and copy all files from ~/touched in it. [ca300@Linuxserver ~]$ mkdir testbackup [ca300@Linuxserver ~]$ cp -r touched testbackup

9. Use one command to remove the directory ~/testbackup and all files in it. [ca300@Linuxserver ~]$ rm -r testbackup

10. Create a directory ~/etcbackup and copy all *.conf files from /etc in it. Did you include all subdirectories of /etc ? [ca300@Linuxserver ~]$ mkdir etcbackup [ca300@Linuxserver ~]$ cp -r /etc/*.conf etcbackup

11. Use rename to rename all *.conf files to *.backup . (if you have more than one distro available, try it on all!) [ca300@Linuxserver ~]$ rename .conf.backup *.conf

File contents
1. Display the first 12 lines of /etc/services. [ca300@Linuxserver ~]$ head -12 /etc/services OUTPUT # /etc/services: # $Id: services,v 1.42 2006/02/23 13:09:23 pknirsch Exp $ # # Network services, Internet style # # Note that it is presently the policy of IANA to assign a single well-known # port number for both TCP and UDP; hence, most entries here have two entries # even if the protocol doesn't support UDP operations. # Updated from RFC 1700, ``Assigned Numbers'' (October 1994). Not all ports # are included, only the more common ones. # # The latest IANA port assignments can be gotten from 2. Display the last line of /etc/passwd. [ca300@Linuxserver ~]$ tail -1 /etc/passwd OUTPUT bz:x:560:562::/home/bz:/bin/bash 3. Use cat to create a file named count.txt that looks like this: One Two Three Four Five [ca300@Linuxserver ~]$ cat > count.txt One Two Three Four Five

4. Use cp to make a backup of this file to cnt.txt. [ca300@Linuxserver ~]$ cp count.txt cnt.txt

5. Use cat to make a backup of this file to catcnt.txt. [ca300@Linuxserver ~]$ cat count.txt > catcnt.txt 6. Display catcnt.txt, but with all lines in reverse order (the last line first). [ca300@Linuxserver ~]$ tac catcnt.txt Five Four Three Two One 7. Use more to display /var/log/messages. [ca300@Linuxserver ~]$ more /var/log/messages 8. Display the readable character strings from the /usr/bin/passwd command. [ca300@Linuxserver ~]$ strings /usr/bin/passwd OUTPUT /lib/ld-linux.so.2 PG~> PTRh QVhP WVSQ 9. Use ls to find the biggest file in /etc. [ca300@Linuxserver ~]$ ls -lrS /etc | tail -1 OUTPUT -rw-r--r-- 1 root root 807103 Jan 7 2007 termcap

10. Open two terminal windows (or tabs) and make sure you are in the same directory in both. Type echo this is the first line > tailing.txt in the first terminal, then issue tail -f tailing.txt in the second terminal. Now go back to the first terminal and type echo This is another line >> tailing.txt (note the double >>), verify that the tail -f in the second terminal shows both lines. Stop the tail -f with Ctrl-C.

Terminal 1 echo this is the first line > tailing.txt Terminal 2 tail -f tailing.txt this is the first line Terminal 1 echo This is another line >> tailing.txt Terminal 2 this is the first line This is another line 11. Use cat to create a file named tailing.txt that contains the contents of tailing.txt followed by the contents of /etc/passwd. [ca300@Linuxserver ~]$ cat /etc/passwd >> tailing.txt 12. Use cat to create a file named tailing.txt that contains the contents of tailing.txt preceded by the contents of /etc/passwd. [ca300@Linuxserver ~]$ mv tailing.txt tmp.txt [ca300@Linuxserver ~]$ cat /etc/passwd tmp.txt > tailing.txt

Filters
1. Put a sorted list of all bash users in bashusers.txt. [ca300@Linuxserver ~]$ grep bash /etc/passwd | cut -d : -f1 | sort > bashusers.txt OUTPUT [ca300@Linuxserver ~]$ cat bashusers.txt 288 admin avinash bz ca263 2. Put a sorted list of all logged on users in onlineusers.txt. [ca300@Linuxserver ~]$ who | cut -d' ' -f1 | sort > onlineusers.txt OUTPUT [ca300@Linuxserver ~]$ cat onlineusers.txt ca271 ca300 ca272 ca290 3. Make a list of all filenames in /etc that contain the string samba. [ca300@Linuxserver ~]$ ls /etc | grep samba OUTPUT samba 4. Make a sorted list of all files in /etc that contain the case insensitive string samba. [ca300@Linuxserver ~]$ ls /etc | grep -i samba | sort OUTPUT samba

5. Look at the output of /sbin/ifconfig. Write a line that displays only ip address and the subnet mask. [ca300@Linuxserver ~]$ ls /sbin/ifconfig | head -2 | grep 'inet' |cut -d : -f3,4 OUTPUT addr:172.16.12.201 Mask:255.255.248.0 6. Write a line that removes all non-letters from a stream. [ca300@Linuxserver ~]$ cat >letter Hello...Happy New Year...*&$#2... [ca300@Linuxserver ~]$ cat letter | tr -d '*&$#' OUTPUT Hello...Happy New Year...2... 7. Write a line that receives a text file, and outputs all words on a separate line. [ca300@Linuxserver ~]$ cat >newtext Today is the last day in 2012 [ca300@Linuxserver ~]$ cat newtext | tr ' ' '\n' OUTPUT Today is the last day in 2012

Users & Groups


1.Create the users Serena Williams, Venus Williams and Justine Henin, all of them with password set to stargate, with username (lower case!) as their first name, and their full name in the comment. Verify that the users and their home directory are properly created. [srividya@servernew ~]$ sudo groupadd sysadmin271 [srividya @servernew ~]$ sudo useradd -m -c sysadmin271 "Serena Williams" serena sudo passwd serena [srividya @servernew ~]$ sudo useradd -m -c sysadmin271 "Venus Williams" venus sudo passwd venus [srividya@servernew ~]$ sudo useradd -m -c sysadmin271 "Justine Henin" justine sudo passwd justine 2.Create a user called bourneuser, give him the Bourne shell (/bin/sh) as his default shell. Log on with this user (on a command line or in a tty) [srividya@servernew ~]$ sudo useradd -s /bin/sh bourneuser passwd bourneuser 3.Create 3 users John,Jane,Eric. Eric doesn`t have an interactive login shell. John & Jane belongs to group as per their username as primary groups and the group " Sysadmin" as secondary group. Eric`s primary group is " sysadmin". Their password will be "password". [srividya@servernew ~]$ sudo groupadd sysadmin [srividya@servernew ~]$ sudo useradd -s /sbin/nologin eric [srividya@servernew ~]$ sudo useradd -G sysadmin john [srividya@servernew ~]$ sudo useradd -G sysadmin jane [srividya@servernew ~]$ sudo usermod -G sysadmin eric [srividya@servernew ~]$ sudo passwd eric 4.Try the commands who, whoami, who am i, w, id, echo $USER $UID ? [ca300@Linuxserver ~]$ who OUTPUT ca290 pts/1 ca271 pts/2 ca300 pts/3 ca300 pts/4 ca315 pts/5 2013-04-29 10:54 (172.16.12.83) 2013-04-29 10:30 (172.16.12.86) 2013-04-29 10:33 (172.16.12.85) 2013-04-29 11:43 (172.16.12.84) 2013-04-29 11:43 (172.16.12.41)

[ca300@Linuxserver ~]$ whoami OUTPUT ca300 [ca300@Linuxserver ~]$ w OUTPUT 11:45:36 up 2:12, 5 users, load average: 0.00, 0.00, 0.00 USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT ca290 pts/1 172.16.12.83 10:54 6.00s 0.14s 0.05s vim hi ca271 pts/2 172.16.12.86 10:30 0.00s 0.18s 0.00s w ca300 pts/3 172.16.12.85 10:33 49.00s 0.13s 0.04s vim q11c.sh ca300 pts/4 172.16.12.84 11:43 5.00s 0.05s 0.05s -bash ca315 pts/5 172.16.12.41 11:43 1.00s 0.02s 0.02s bash [ca300@Linuxserver ~]$ id OUTPUT uid=540(ca300) gid=540(ca300) groups=540(ca300) [ca300@Linuxserver ~]$ echo $USER $UID OUTPUT ca300 540

5. Put a file in the skeleton directory and check whether it is copied to user's home directory. When is the skeleton directory copied ? [srividya@servernew ~]$ touch test.txt [srividya@servernew ~]$ mv test.txt /etc/skel [srividya@servernew ~]$ sudo useradd p [srividya@servernew ~]$ passwd p [srividya@servernew ~]$ su p [p@servernew ~]$ ls OUTPU T test.txt

6. Create the groups tennis, football and sports. [srividya@servernew ~]$ groupadd tennis [srividya@servernew ~]$ groupadd football [srividya@servernew ~]$ groupadd sports

7. In one command, make venus a member of tennis and sports. [srividya@servernew ~]$ usermod -a -G tennis,sports venus 8. Rename the football group to foot. [srividya@servernew ~]$ groupmod -n foot football 9. Use the id command to verify that serena is a member of tennis [ca300@Linuxserver ~]$ id uid=540(ca300) gid=540(ca300) groups=540(ca300)

Files & Permissions


1. As normal user, create a directory ~/permissions. Create a file owned by yourself in there. mkdir ~/permissions [ca300@Linuxserver permissions]$ touch myfile1.txt 2. Copy a file owned by root from /etc/ to your permissions dir, who owns this file now ? cd permissions cp /etc/hosts ~/permissions 3. As root, create a file in the users ~/permissions directory. [ca300@Linuxserver permissions]$ touch myfile1.txt 4. As normal user, look at who owns this file created by root. ls -l ~/permissions total 4 -rw-r--r-- 1 ca300 ca300 187 Jan 18 10:40 hosts -rw-rw-r-- 1 ca300 ca300 0 Jan 18 10:39 myfile1.txt -rw-rw-r-- 1 ca300 ca300 0 Jan 18 10:43 myfile2.txt 5. Change the ownership of all files in ~/permissions to yourself. chown ca300 ~/permissions/* 6. Make sure you have all rights to these files, and others can only read. cd permissions [ca300@Linuxserver permissions]$ chmod 644 myfile1.txt [ca300@Linuxserver permissions]$ ls -l total 4 -rw-r--r-- 1 ca300 ca300 187 Jan 18 10:40 hosts -rw-r--r-- 1 ca300 ca300 0 Jan 18 10:39 myfile1.txt -rw-rw-r-- 1 ca300 ca300 0 Jan 18 10:43 myfile2.txt cd .. [ca300@Linuxserver ~]$ chmod 755 permissions [ca300@Linuxserver ~]$ ls -l 7a. Display the umask in octal and in symbolic form. umask 0002 [ca300@Linuxserver ~]$ umask -S u=rwx,g=rwx,o=rx [ca300@Linuxserver ~]$

7b. Set the umask to 077, but use the symbolic format to set it. Verify that this works. umask u=rwx,g=,o= [ca300@Linuxserver ~]$ umask 0077 8. Create a directory that belongs to a group, where every member of that group can read and write to files, and create files. Make sure that people can only delete their own files. root@deb503:~# mkdir /home/ca285/grpdirectory root@deb503:~# groupadd .grpdirectory ss root@deb503:~# chmod 1760 grpdirectory

Special File Permissions


1a. Set up a directory, owned by the group sports. groupadd sports mkdir /home/sports chown root:sports /home/sports 2. Verify the permissions on /usr/bin/passwd. Remove the setuid, then try changing your password as a normal user. Reset the permissions back and try again. root@deb503:~# ls -l /usr/bin/passwd -rwsr-xr-x 1 root root 31704 2009-11-14 15:41 /usr/bin/passwd root@deb503:~# chmod 755 /usr/bin/passwd root@deb503:~# ls -l /usr/bin/passwd -rwxr-xr-x 1 root root 31704 2009-11-14 15:41 /usr/bin/passwd A normal user cannot change password now. root@deb503:~# chmod 4755 /usr/bin/passwd root@deb503:~# ls -l /usr/bin/passwd -rwsr-xr-x 1 root root 31704 2009-11-14 15:41 /usr/bin/passwd

3. Create a group Alpha with team members Avinash ,Akhil, Ajay. Create a directory for the Group as Alpha .Any file created by group members in this directory will be having the group as it`s group.Other`s have no write permission on this directory.ie All files and executable`s in this directory will have the group privilege the 1st preference. Restrict users from deleting the files which are not owned by them. root@deb503:~# groupadd Alpha root@deb503:~#useradd g Alpha Avinash root@deb503:~#passwd avinash root@deb503:~#useradd g Alpha Akhil root@deb503:~# passwd akhil root@deb503:~#useradd g Alpha Ajay root@deb503:~#passwd ajay root@deb503:~#mkdir alpha root@deb503:~#chgrp Alpha alpha root@deb503:~#chmod 2775 alpha root@deb503:~#chmod 1775 alpha

File Compression
1) Create a directory name system and copy the contents of /etc directory to this directory and apply all compressing tools(tar,gzip,bzip,compress) on this directory. Also decompress the same. [ca300@Linuxserver system]$ cp -r /etc ~/system [ca300@Linuxserver system]$ compress system [ca300@Linuxserver system]$uncompress system [ca300@Linuxserver etc]$ gzip mailcap [ca300@Linuxserver etc]$ gunzip system.gz [ca300@Linuxserver etc]$ bzip2 system [ca300@Linuxserver etc]$ bunzip2 system.bz2 [ca300@Linuxserver etc]$ tar -cf ss.tar system [ca300@Linuxserver etc]$ tar -xvf ss.tar

Shell Scripting
1)Write a shell script (using vi editor) to create a directory test in your area and copy the contents of /etc in test directory #! /bin/bash mkdir test cp /etc/*.* test OUTPUT cp: cannot open `/etc/aliases.db' for reading: Permission denied cp: cannot open `/etc/at.deny' for reading: Permission denied cp: cannot open `/etc/autofs_ldap_auth.conf' for reading: Permission denied cp: omitting directory `/etc/cron.d' cp: omitting directory `/etc/cron.daily'

2)Write a shell script to add two user given values ? #! /bin/bash echo "Enter the first number" read a echo "Enter the second number" read b echo $(($a+$b)) OUTPUT [ca300@Linuxserver shellscript]$ vi q2.sh [ca300@Linuxserver shellscript]$ bash q2.sh Enter the first number 5 Enter the second number 2 7

3)Find Area & Perimeter of the following a)Circle b)Rectangle c)Parallelogram d)Triangle e)Cylinder Inputs are accepted from the user circle [ca300@Linuxserver shellscript]$ vi 3a.sh #! /bin/bash # circle echo "Enter the radius" read r echo "area is" echo "3.14*$r*$r"|bc echo "perimeter is" echo "2*3.14*$r"|bc OUTPUT [ca300@Linuxserver shellscript]$ bash 3a.sh Enter the radius 1 area is 3.14 perimeter is 6.28 rectangle [ca300@Linuxserver shellscript]$ vi 3b.sh #! /bin/bash echo "Enter the length and breadth of rectangle" read a read b echo "area is" echo $(($a*$b)) echo "perimeter is" l=$(($a+$b)); echo $((2*$l))|bc

OUTPUT [ca300@Linuxserver shellscript]$ bash 3b.sh Enter the length and breadth of rectangle 2 3 area is 6 perimeter is 10 parallelogram #! /bin/bash echo "Enter the base" read base echo "Enter the height" read height echo "Enter the side1,side2&side3" read side1 read side2 read side3 echo "The area is" a=$(($base*$height)) echo "$a/2"|bc echo "the perimeter is" echo $(($side1+$side2+$side3)) OUTPUT [ca300@Linuxserver shellscript]$ bash 3c.sh Enter the base 2 Enter the height 5 Enter the side1,side2&side3 5 6 3 The area is 5 the perimeter is 14

Triangle #! /bin/bash echo "Enter the length of the base" read b echo "Enter the altitude" read a echo "Enter the side length" read h echo "area is" echo $(($b*$a))|bc echo "perimeter is" echo $((2*$b+$h)) OUTPUT [ca300@Linuxserver shellscript]$ bash 3d.sh Enter the length of the base 5 Enter the altitude 6 Enter the side length 4 area is 30 perimeter is 14 Cylinder #!/bin/bash echo "radius " read r echo "enter height" read h a=$(echo "scale=2;(3.14 * $r * $r * $h) "|bc) peri=$(echo "scale=2;((2 * 3.14 * $r *$r * $h) + (2 * 3.14 * $r * $h )) "|bc) echo "volume :" $a echo "surface area:" $peri

OUTPUT [ca300@Linuxserver shellscript]$ bash 3e.sh radius 6 enter height 7 volume : 791.28 surface area: 1846.32 4)Write a script to list all the files with their complete details in the current directory #! /bin/bash echo `ls -l` OUTPUT [ca300@Linuxserver shellscript]$ bash 4.sh total 68 -rw-rw-r-- 1 ca300 ca300 129 Apr 24 10:13 3a.sh -rw-rw-r-- 1 ca300 ca300 159 Apr 24 10:14 3b.sh -rw-rw-r-- 1 ca300 ca300 259 Apr 24 10:15 3c.sh -rw-rw-r-- 1 ca300 ca300 197 Apr 24 10:15 3d.sh -rw-rw-r-- 1 ca300 ca300 27 Apr 24 10:15 4.sh -rw-rw-r-- 1 ca300 ca300 168 Apr 24 10:18 q11b.sh -rw-rw-r-- 1 ca300

5)Write a shell script to check whether entered year is leap year or not ? #! /bin/bash echo "enter the year" read y if [[ $(($y % 4)) == 0 && $(($y % 100)) != 0 ]] || [ $(($y % 400)) == 0 ]echo "leap year" else echo "not a leap year" fi OUTPUT [ca300@Linuxserver shellscript]$ bash q6.sh enter the year 2000 leap year

6)Write a shell script to check whether the entered string is palindrome or not (without actually reversing. echo "Enter a string to be entered:" read str echo len=`echo $str | wc -c` len=`expr $len - 1` i=1 j=`expr $len / 2` while test $i -le $j do k=`echo $str | cut -c $i` l=`echo $str | cut -c $len` if test $k != $l then echo "String is not palindrome" exit fi i=`expr $i + 1` len=`expr $len - 1` done echo "String is palindrome" OUTPUT [ca300@Linuxserver shellscript]$ bash q7.sh Enter a string to be entered: amma String is palindrome 7)Write a shell script to check whether a file is readable, writable or executable? If writable provision should be given to enter details? #!/bin/bash echo "Enter a file name" read n if [ -r $n ]; then echo "Read permission" elif [ -x $n ]; then echo "Execute permission" elif [ -w $n ]; then echo "Write permission" echo "Enter details" read id

read name echo $id echo $name else echo "invalid" fi OUTPUT [ca300@Linuxserver shellscript]$ bash q8.sh Enter a file name hello Read permission 8)Write a shell script to show various system configurations like (i) Currently logged user and his log name (ii) Your current shell (iii) Your home directory (iv) Your Operating System type (v) Your current path setting (vi) Your current working directory (vii) Show currently logged number of users (viii) Show all available shells #!/bin/bash echo -n "Currently logged user and his log name " echo $LOGNAME echo -n "Current shell" echo $SHELL echo -n "Home directory" echo $HOME echo -n "current path setting" echo $PATH echo -n "Hostname" echo $HOSTNAME echo -n "Present working directory " echo $PWD echo -n "OSTYPE" echo $OSTYPE echo "Types of shells" echo "`cat /etc/shells`"

OUTPUT [ca300@Linuxserver shellscript]$ vi q9a.sh [ca300@Linuxserver shellscript]$ bash q9a.sh Currently logged user and his log name ca300 Current shell/bin/bash Home directory/home/ca300 current path setting/usr/kerberos/bin:/usr/local/bin:/bin:/usr/bin:/home/ca300/bin HostnameLinuxserver.sstm.in Present working directory /home/ca300/shellscript OSTYPElinux-gnu Types of shells /bin/sh /bin/bash /sbin/nologin /bin/tcsh /bin/csh /bin/ksh /bin/zsh

9)Write a shell script called see taking a filename as argument which ls's the file if it's a directory and more's the file otherwise. #! /bin/bash echo "enter file name" read i if [ -d $i ]; then ls -l $i echo "directory" else more $i echo "file" exit 1 fi OUTPUT [ca300@Linuxserver shellscript]$ bash list enter file name first echo hi srividya.

10)Write a shell script to accept a 5 digit number a)Sum of Digits b)Reverse it c)Check whether it`s palindrome a)Sum of Digits #! /bin/bash echo "Enter the number" read n let sum=0 while [ $n -gt 0 ]; do rem=$(($n%10)) sum=$(($sum+$rem)) n=$(($n/10)) done echo "sum is" echo $sum OUTPUT [ca300@Linuxserver shellscript]$ bash q11.sh Enter the number 123 sum is 6 b)Reverse it #! /bin/bash echo "Enter the number" read n rev=0 while [ $n -gt 0 ]; do rem=$(($n%10)) r1=$(($rev*10)) rev=$(($r1+$rem)) n=$(($n/10)) done echo "reverse is" echo $rev OUTPUT [ca300@Linuxserver shellscript]$ bash q11b.sh Enter the number 451 reverse is 154

c)Check whether it`s palindrome #! /bin/bash echo "Enter the number" read n rev=0 let dup=n while [ $n -gt 0 ]; do rem=$(($n%10)) r1=$(($rev*10)) rev=$(($r1+$rem)) n=$(($n/10)) done if [ $dup -eq $rev ]; then echo "palindrome" else echo "not" fi OUTPUT [ca300@Linuxserver shellscript]$ bash q11c.sh Enter the number 121 Palindrome

11)Write a shell script to find factorial of a given number? #! /bin/bash i=1 fact=1 echo "ENTER A NUMBER" read a while [ $i -le $a ] do fact=$(( $fact * $i)) i=$(($i+1)) done echo "THE FACTORIAL IS" echo $fact OUTPUT [ca300@Linuxserver shellscript]$ bash q12.sh ENTER A NUMBER 5 THE FACTORIAL IS 120

12)Write a shell script to search for a pattern in a given file. #!/bin/bash echo "To search a given a pattern" ls /etc | grep samba OUTPUT [ca300@Linuxserver shellscript]$ bash q15.sh samba 13)Write a shell script to simulate a simple calculator program to perform addition subtraction multiplication division and modulus? echo "1.ADDITION" echo "2.SUBTRACTION" echo "3.MULTIPLICATION" echo "4.DIVISION" echo "5.MODULUS" echo "Enter first number" read a echo "Enter second number" read b echo "Enter your choice" read choice case $choice in 1) c=$(($a+$b)) echo "The sum is" $c;; 2) c=$(($a-$b)) echo "The difference is" $c;; 3) c=$(($a*b)) echo "The product is" $c;; 4) c=$(($a/$b)) echo "The quotient is" $c;; 5) c=$(($a&b)) echo "The remainder is" $c;; *) echo "PLZ ENTER VALID OPTION";; esac OUTPUT [ca300@Linuxserver shellscript]$ bash q14.sh 1.ADDITION 2.SUBTRACTION 3.MULTIPLICATION 4.DIVISION 5.MODULUS Enter first number 1

Enter second number 2 Enter your choice 1 The sum is 3 14)Write a shell script which displays a list of all files in the current directory to which you have read, write and execute permissions? #! /bin/bash for File in * do if [ -r $File -a -w $File -a -x $File ]; then echo $File fi done OUTPUT [ca300@Linuxserver sree1]$ bash file3 a area area1 eg1 hai pgm1

Shell Scripting (Pattern Drawing )


a)
* * * * * * * * * *

#!/bin/bash echo "Enter the limit" read n for(( i=1; i<=$n; i=$i+1 )) do for(( j=1;j<=i;j=$j+1 )) do echo -n "* " done echo "" done OUTPUT [ca300@Linuxserver ~]$ bash pat2.sh Enter the limit 5 * ** *** **** *****

b) 1 12 123 1234 12345 #!/bin/bash echo "Enter the limit" read n for(( i=1; i<=$n; i=$i+1 )) do for(( j=1;j<=i;j=$j+1 )) do echo -n "$j" done echo "" done OUTPUT [ca300@Linuxserver ~]$ bash pat1.sh Enter the limit 5 1 12 123 1234 12345

c) 1 22 333 4444 55555 #!/bin/bash echo "Enter the limit" read n for(( i=1; i<=$n; i=$i+1 )) do for(( j=1;j<=i;j=$j+1 )) do echo -n "$i" done echo "" done OUTPUT [ca300@Linuxserver ~]$ bash pat3.sh Enter the limit 5 1 22 333 4444 55555

d) |_ | | | | |_ | |_ | | |_ | | | |_

#!/bin/bash echo "Enter the limit" read n for(( i=1; i<=$n; i=$i+1 )) do for(( j=1;j<=i;j=$j+1 )) do echo -n " |" done echo "_" done OUTPUT [ca300@Linuxserver ~]$ bash pat4.sh Enter the limit 5 |_ | |_ | | |_ | | | |_ | | | | |_

e)
* * * * * * * * * * * * * * * * * *

* * * * * *

* * * * * *

#!/bin/bash echo "Enter the limit" read n for(( i=1; i<=$n; i=$i+1 )) do for(( j=1;j<=i;j=$j+1 )) do echo -n " * " done echo "" done for(( i=$n; i>=1; i=$i-1 )) do for(( j=1;j<=i;j=$j+1 )) do echo -n " * " done echo "" done OUTPUT [ca300@Linuxserver ~]$ bash pat6.sh Enter the limit 5 * ** *** **** ***** ***** **** *** ** *

f)
* *** ***** ******* ********* *********** *************

#!/bin/bash echo "Enter the limit" read n for((i=1;i<=$n;i=$i+1)) do for((j=1;j<=$n-$i;j=$j+1)) do echo -n " " done for((j=1;j<=2*$i-1;j=$j+1)) do echo -n "*" done echo " " done OUTPUT [ca300@Linuxserver]$ bash pattern Enter the limit 10 * *** ***** ******* ********* *********** ************* *************** ***************** *******************

g) 1 222 33333 4444444 echo "Enter the limit" read n for((i=1;i<=$n;i=$i+1)) do for((j=1;j<=$n-$i;j=$j+1)) do echo -n " " done for((j=1;j<=2*$i-1;j=$j+1)) do echo -n "$i" done echo " " done OUTPUT [ca300@Linuxserver]$ bash pattern1 Enter the limit 5 1 222 33333 4444444 555555555

h) * *** ***** ******* ********* ********* ******* ***** *** * #!/bin/bash echo "Enter the limit" read n for((i=1;i<=$n;i=$i+1)) do for((j=1;j<=$n-$i;j=$j+1)) do echo -n " " done for((j=1;j<=2*$i-1;j=$j+1)) do echo -n "*" done echo " " done for((i=$n;i>=1;i=$i-1)) do for((j=1;j<=$n-$i;j=$j+1)) do echo -n " "

OUTPUT [ca300@Linuxserver]$ bash pattern2 Enter the limit 5 * *** ***** ******* ********* ********* ******* ***** *** *

Parameter Handling
1)Write a shell script to add n user given values ? #!/bin/bash sum=0 for i in $* do sum=`expr $sum + $i` done echo "Summation of "$#" no. is: "$sum OUTPUT [ca300@Linuxserver ~]$ bash pa1.sh 10 20 Summation of 2 no. is: 30 2)Write a shell script to check whether entered year is leap year or not ? If it`s leap year printout the calender for the month February (give its as an option -p)? #!/bin/bash if [[ $(($1 % 4)) == 0 && $(($1 % 100)) != 0 ]] || [ $(($1 % 400)) == 0 ]echo "Leap Year" cal 2 $* else echo "not leap year" fi OUTPUT [ca300@Linuxserver ~]$ bash pa2.sh 2000 Leap Year February 2000 Su Mo Tu We Th Fr Sa 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29

3)Write a shell script to check whether the entered string is palindrome or not ? #!/bin/bash echo len=`echo $* | wc -c` len=`expr $len - 1` i=1 j=`expr $len / 2` while test $i -le $j do k=`echo $* | cut -c $i` l=`echo $* | cut -c $len` if test $k != $l then echo "String is not palindrome" exit fi i=`expr $i + 1` len=`expr $len - 1` done echo "String is palindrome" OUTPUT [ca300@Linuxserver ~]$ bash pa3.sh level String is palindrome [ca300@Linuxserver ~]$ bash pa3.sh mid String is not palindrome

4)Write a shell script to check whether a file is readable, writable or executable? If writable data passed as argument should be written on to the file? #!/bin/bash if [ -r $* ]; then echo "Read permission" elif [ -x $* ]; then echo "Execute permission" elif [ -w $* ]; then echo "Write permission" else echo "invalid" fi

OUTPUT [ca300@Linuxserver ~]$ bash pa4.sh text Write permission [ca300@Linuxserver ~]$ bash pa4.sh text1 Read permission 5)The status of calls attended by customer care executives for a month is being logged into a file that follows the naming convention monthlycallsmmyyyy where mm denotes the month and yyyy denotes the year Data format of the file monthlycalls042013 is as follows: EMPID:EMPNAME:CALLSRECEIVED:CALLSATTENDED:CALLSPENDING 1010 : ABY : 150 : 50 : 100 -------You need to create a shell script call monthly report to analyze data for the month. It should fulfill following requirements: 1. If no parameter is passed to the shell script, it should display an error message.It should also display help that explains how to use the shell script.(-h option) 2.It should accept two parameters (-s or -a) 3. If parameter is -s, then it should calculate total number of calls received for the month by an executive. 4. If parameter is -a, then it should calculate the total number of calls attended for that month. #!/bin/bash if [ $# -lt 2 ]; then echo "No parameter passed " elif [ $1 = "-s" ]; then sum=0 for i in `cut -d":" -f3 $2` do sum=$(($i + $sum)) done echo "total number of calls received for the month "$sum elif [ $1 = "-a" ]; then sum=0 for i in `cut -d":" -f4 $2`

do sum=$(($i + $sum)) done echo " total number of calls attended for that month " $sum else echo "Invalid option" fi OUTPUT [ca300@Linuxserver ~]$ bash pa5.sh -a monthlycalls042013 total number of calls attended for that month 250 [ca300@Linuxserver ~]$ bash pa5.sh -s monthlycalls042013 total number of calls received for the month 500 [ca300@Linuxserver ~]$ cat monthlycalls042013 1000:ABY:150:50:100 1010:EBY:200:100:100 1020:EMY:150:100:50

Scheduling
1)

Let the user backup a directory /home on to /tmp/data/backup everyday at 5pm without logging into the machine. [ca300@Linuxserver ~]$ crontab e 00 17 * * * cp /home/ca300 /tmp/data/backup OUTPUT [ca300@Linuxserver ~]$ mail Mail version 8.1 6/6/93. Type ? for help. "/var/spool/mail/ca300": 3 messages 3 new 2) Using a proper scheduling mechanism schedule following tasks a) Execute a shell script which backups all the user files in their home directory to /backup/users/ at every day 5:00PM except Sunday? [ca300@Linuxserver ~]$ crontab e 00 17 * * * 1-6 cp /home/ca300 /backup/users OUTPUT [ca300@Linuxserver ~]$ mail Mail version 8.1 6/6/93. Type ? for help. "/var/spool/mail/ca300": 3 messages 3 new b) Schedule a job which executes a shell script which deletes all files in the home directory at midnight of 31st of December 2013. #!/bin/bash rm -r ~/backup/users OUTPUT [ca300@Linuxserver sree1]$ at -f 13q2b midnight 12/31/2013 job 75 at 2013-12-31 00:00

Potrebbero piacerti anche