Page 1 :
Basic Script Building, , , , K, , | i mmands, i ipt fi i i :, | using multiple commands, Creating script files, Displaying messages, Using variables, Redirecting I, i performing math, Exiting the script. ‘ ig IIPHE SoH Out eae PSS, , , , , , 3.1 Using Multiple Commands, , , , , , Q. Explain with examples the working of multiple commands. 5 |, , « Through shell we can allow multiple commands in a single step., , Fore.g,, , , , , ( : s@ubuntu:, cs@ubuntu:~$ date ; who, Fri Oct 8 22:29:55 PDT 2021, , cs 20 2021-10-08 22:00 (:0), cs pts/12 2021-10-08 22:00 (:0), cs@ubuntu:~$, , * A shell script is defined as it is plain text file with a set of Linux command, flow of control and Input Output, , facilities. And it is created by using any text editor like vi,emac etc., , file and directory management features and it interpreted directly. Shell script, , * Shell script allows use of variable,, , provides many features like loop,, put and parsing the command line. Shell Supports advance, , constructs, arrays, functions, logic with other utilities etc., * Shell script allows reading in ed features such as, , Functions and Arrays, regular expressions., , Easy to use and understand., * Itismuch quicker than programming in any other languages., , , , B14 Shell Script, , | Q. Write syntax of shell script with example., , , , , , Following is syntax/ basic structure of shell script., , Syntax ieee, , #1/bin/bash (Shebang), # (comments)
Page 2 :
WF Linux operating System (MU_B.Sc) 8-2 Basic Spb,, echo “ ” (To print message of variables contains) ‘ |, -/scriptfilename.sh (Execute script) : | |, , Where,, #!/bin/bash :- it define which shell will be used to run the shell script., #comments - by using ‘#’ symbol you can pass the comments., , chmod +x scriptfilenamesh - it define file is executable and tell to Linux that file is executable., , ./scriptfilename.sh -it define execute the script., , Examples, To check your current shell use following command as, , echo $SHELL, Here, dollar sign ($) stands for a shell variable, , 8.2 Creating Script Files, Construct shell script you can use text editor such as vi or cat command., , 1) vicommand, , First type following vi command and rest of text as, , hemant1@ubuntu:~$ vi demofile.sh, , , , # this is my first program, echo “we learn basic shell scripting”, , hemant1@ubuntu:~$ chmod +x demofile.sh, , , , hemant1@ubuntu:~$ ./demofile1.sh, , Output, We learn basic shell scripting, following are screen snaps for creating first shell script program., , , , , hemanti@ubuntu: ~$ vi demofile.sh, , , , hemant1@ubuntu:~$ vi demofile.sh, , # this is my first program, -echo**We learn basic shell scripting”, , , , 2) cat command, , First type following cat command and rest of text as, , , , acai
Page 3 :
Syste J_B.Sc.), * x Operating System (MU_ 8-3, Wy Linw Basic Script Building, , janti@ubuntur~$ cat >demofile2.sh, , , , , , , , hem, # pri, , echo, , int your name using input from keyboard, "enter your name"read name, echo "your name is:"$name, , When you finished your writing script press CTR+D to save and then by using chmod command give =, . execu! e, , permission to file., hemant] @ubuntu:~$ chmod +x demofilez.sh, , For execute file give command as ./demofile2.sh, hemant1 @ubuntu:~$ ./demofile2.sh, enter your name, , ashwinl, , your name is:ashwini, , , , hemant1@ubuntu:~$, , « Sometimes arguments are specified with shell procedure ten they are assigned to special variable or positional, parameters., 1) $1,$2,$3 The positional parameters., 2) $*The complete set of positional parameters as single string., 3) $# The number of arguments specified in command line., 4) $0 Name of executed command., 5) $? Exit status of the last command, 6) $!PID of last command, , Example, , hemant1 @ubuntu:~$ cat >scripfile.sh, # Example to print standard input from user to standard output using parameters, €cho "Program:$0 The No.Of arguments are:$# The arguments are:$*", , cho "Your name is:$1\n Your number is:$2", When you finished your script press CTR+D to save, hemant] @ubuntu:~$ chmod +x scripfile.sh, , by using chmode command give executable permission to file., , , , t, ~<mant] @ubuntu:~$ ,/scripfile.sh ashwini 18889, , Here we pass optional parameters as ashwini and 18889 as shown above., | Output, , henantigubuntu: $ .fscrt, , sw$ . pfile.sh ashwint 18889 es ae, Program:./scripfile.sh The No.Of arguments are:2 The acgunents areiashwint 188, Your nane ts:ashwini\n Your number ts:18889, , henanti@ubuntu:~$ ;
Page 4 :
=), ; 8-4 Bagi |, BF Linux Operating system (MU_BSc) Seri, 8.3 Displaying Messages, [a Which command is used to display the messages? Explain it with examples. |, ‘ollowing is the example of echo commang f, , , , , , , We can display text strings by using the echo command. F, , Serr, , , , , , , , , , felts tl as, , cs@ubuntu:~$ echo "This is Linux Class", This is Linux Class, cs@ubuntu:~$, , , , cs@ubuntu: ~§ cat echol.sh, echo The Time and Date are, , date, , echo Following user is logged into the system, who, , lecho Display content of batch.txt, , cat batch. txt, , cs@ubuntu:~$ ff, , |, , Output :, , , , cs@ubuntu:~$ sh echoi.sh, , The Time and Date are, Fri Oct 8 22:44:48 PDT 2021, , Following user is logged into the system, , Ics 26 2021-10-08 22:00 (:0), Ics pts/12 2021-10-08 22:60 (:6), Display content of batch. txt, , Hello World, , cs@ubuntu:~$ a, , , , The above output shows the date and time, who is logged in the, “patch.txt”., , ie, system and it also displays the content of hel, , 8.4 Using Variables, , , , Variables stores information in shell script temporarily which we can further use j th ipt., ‘ in the script., , , , et