shell scripting notes

18
Setting priority on new processes At this point you are probably wondering how you can set your own priority levels on processes. To change the priority when issuing a new command you do nice -n [nice value] [command]: nice -n 10 apt-get upgrade This will increment the default nice value by a positive 10 for the command, ‘apt-get upgrade‘ This is often useful for times when you want to upgrade apps but don’t want the extra process burden at the given time. Remember a positive number is gives less priority for a process. Setting Priority on Existing Processes Obviously at some point you are going to want to alter the nice value of something that is already running. Just the other day I was doing an upgrade to Ubuntu Jaunty and Firefox started to become unusably slow. A quick renice command rescheduled the upgrade to a lower priority and I was back to surfing in no time. To change the priority of an existing process just do renice [nice value] -p [process id]: renice 10 -p 21827

Upload: jai-sharma

Post on 17-Aug-2015

212 views

Category:

Documents


0 download

DESCRIPTION

ddggh

TRANSCRIPT

Setting priority on new processesAt this point you are probably wondering how you can set your own priority levels on processes. To changethe prioritywhen issuing a new command you do nice -n [nice value] [command]:nice -n 10 apt-get upgradeThis will increment the default nice value by a positive 10 for the command, apt-get upgrade This is often useful for times when you want to upgrade apps but dont want the extra process burden at the given time. Remember a positive number is gives less priority for a process.Setting Priority on Existing ProcessesObviously at some point you are going to want to alter the nice value of something that is already running. Just the other day I was doing an upgrade to Ubuntu Jaunty and Firefox started to become unusably slow. A quick renice command rescheduled the upgrade to a lower priority and Iwas back to surfng in no time.To change the priority of an existing process just do renice [nice value] -p [process id]:renice 10 -p 21827This will increment the priority of the process with an id of 21827 to 10.Note: Only root can apply negative nice values.Shell scripting: The shell provides you with an interface to the UNIX system. It gathers input from you and executes programs based on that input. A shell is an environment in which we can run our commands, programs, and shell scripts. There are diferent favors of shells.Shell prompt : The prompt, $, which is called command prompt, is issued by the shell. While the prompt is displayed, you can type a command. For eg: $ dateCase -esac programinshell scripting :Syntax :case word inpattern1) Statement(s) to be executed if pattern1 matches ;;pattern2) Statement(s) to be executed if pattern2 matches;;pattern3) Statement(s) to be executed if pattern3 matches;;EsacExample 1 :echo Enter any value between 1 to 5read numcase $num in1)echo ONE ;;2)echo TWO;;3)echo THREE;;4)echo FOUR;;5)echo FIVE;;*)echo INVALID VALUE;;esacNote:* indicates default value.If else :Comparisons: -eqequal to-nenot equal to-ltless than-leless than or equal to-gtgreater than-gegreater than or equal toIf -elseprogram :Syntax :if [ expression ]then Statement(s) to be executed if expression is trueelse Statement(s) to be executed if expression is not truefExample 2 :valid_password=welcomeEcho Enter your passwordreadPSWDif [ $PSWD = $valid_password ]thenecho Valid passwordelseecho Access deniedfExample 3 :Echo Enter the numberRead numIf test$num le 30Thenecho Number is less than equal to 30elseecho Number is greater than 30fif then elif :Syntax :if [ expression 1 ]then Statement(s) to be executed if expression 1 is trueelif [ expression 2 ]then Statement(s) to be executed if expression 2 is trueelif [ expression 3 ]then Statement(s) to be executed if expression 3 is trueelse Statement(s) to be executed if no expression is truefExample 4 :echo enter the value of aread aecho enter the value of bread bif [ $a = $b ]thenecho a is equal to belif [ $a gt $b ]thenecho a is greater than belif [ $a lt $b ]thenecho a is less than belseecho INVALID VALUEfWhileloop :Syntax :while commanddo Statement(s) doneExample 5 :echo Enter the value of aread awhile [ $a -lt 5 ]do a=`expr $a + 1` echo $adoneForloop :Syntax :for (( EXP1; EXP2; EXP3 ))docommand1command2command3doneExample 6 :for((c=1;cReturns True, if both condition1 and condition2 are True (Like AND operator)|| -------- >Returns True, If any one condition is True (Like OR operator)Boolean Operators:There are following boolean operators supported by BourneShell.Assume variable a holds 10 and variable b holds 20 then:Operator Description Example!This is logical negation. This inverts a true condition into false and vice versa.[ ! false ] is true. -oThis is logical OR. If one of the operands is true then condition would be true.[ $a -lt 20 -o $b -gt 100 ] is true. -a This is logical AND. If both [ $a -lt 20 -a $b -gt the operands are true then condition would be true otherwise it would be false.100 ] is false. Example 11: (For || operator)echo Enter the value of aread aecho Enter the value of bread bif (( a == 1 )) | | (( b == 8 ));thenecho Enable!!!elseecho DisablefFILETESTOPERATORS :The diferent fle test operators are listed below:a : True if the fle exists.b : True if the fle exists and is a block special fle.c : True if the fle exists and is a character special fle.d : True if the fle exists and is a directory.e : True if the fle exists.f : True if the fle exists and is a regular fle.g : True if the fle exists and its SGID bit is set.h : True if the fle exists and is a symbolic link.k : True if the fle exists and its sticky bit is set.p : True if the fle exists and is a named pipe (FIFO).r : True if the fle exists and is readable.s : True if the fle exists and has a size greater than zero.t : True if fle descriptor is open and refers to a terminal.u : True if the fle exists and its SUID (set user ID) bit is set.w : True if the fle exists and is writable.x : True if the fle exists and is executable.O : True if the fle exists and is owned by the efective user ID.G : True if the fle exists and is owned by the efective groupID.L : True if the fle exists and is a symbolic link.N : True if the fle exists and has been modifed since it waslast read.S : True if the fle exists and is a socket.Syntax : (mostly used in if-loop)if [ -option flename ]thendo somethingelsedo somethingfExample 12 :(bud.sh is an already existing fle)fle=bud.shIf [-r $fle ] (similarly, for write if [ -w $fle ] and for execute if [ -x $fle ] )ThenEcho Read accessElseEcho Read permission deniedfExample13 :File=directory1If [ -d $fle ]ThenEcho Directory is presentElseEcho Directory is not presentfDebugging Shell Scripts:-x option to debug a shell scriptRun a shell script with -x option.$ Bash -x script-name$ bash -x domains.shUse of set built in commandBash shell ofers debugging options which can be turn on or of using set command.Set -x:Display commands and their arguments as they are executed.Set -v: Display shell input lines as they are read.How to execute:Bash x flename.shBash v flename.sh