Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 9
Run it as follows:
Your first name please: vivek
Hello vivek, Lets be friend! Prev Home Next Exit Status Up Wild cards (Filename Shorthand or meta Characters) LSST v1.05r3 > Chapter 2 > The read Statement http://www.cyberciti.biz/pdf/lsst/ch02sec10.html [7/29/2002 6:51:56 PM] Linux Shell Scripting Tutorial (LSST) v1.05r3 Prev Chapter 2: Getting started with Shell Programming Next Wild cards (Filename Shorthand or meta Characters) Wild card /Shorthand Meaning Examples * Matches any string or group of characters. $ ls * will show all files $ ls a* will show all files whose first name is starting with letter 'a' $ ls *.c will show all files having extension .c $ ls ut*.c will show all files having extension .c but file name must begin with 'ut'. ? Matches any single character. $ ls ? will show all files whose names are 1 character long $ ls fo? will show all files whose names are 3 character long and file name begin with fo [...] Matches any one of the enclosed characters $ ls [abc]* will show all files beginning with letters a,b,c Note: [..-..] A pair of characters separated by a minus sign denotes a range. Example: $ ls /bin/[a-c]* Will show all files name beginning with letter a,b or c like /bin/arch /bin/awk /bin/bsh /bin/chmod /bin/cp /bin/ash /bin/basename /bin/cat /bin/chown /bin/cpio /bin/ash.static /bin/bash /bin/chgrp /bin/consolechars /bin/csh But $ ls /bin/[!a-o] $ ls /bin/[^a-o] If the first character following the [ is a ! or a ^ ,then any character not enclosed is matched i.e. do not show us file name that beginning with a,b,c,e...o, like LSST v1.05r3 > Chapter 2 > Wild cards (Filename Shorthand or meta Characters) http://www.cyberciti.biz/pdf/lsst/ch02sec11.html (1 of 2) [7/29/2002 6:51:58 PM] /bin/ps /bin/rvi /bin/sleep /bin/touch /bin/view /bin/pwd /bin/rview /bin/sort /bin/true /bin/wcomp /bin/red /bin/sayHello /bin/stty /bin/umount /bin/xconf /bin/remadmin /bin/sed /bin/su /bin/uname /bin/ypdomainname /bin/rm /bin/setserial /bin/sync /bin/userconf /bin/zcat /bin/rmdir /bin/sfxload /bin/tar /bin/usleep /bin/rpm /bin/sh /bin/tcsh /bin/vi Prev Home Next The read Statement Up More command on one command line LSST v1.05r3 > Chapter 2 > Wild cards (Filename Shorthand or meta Characters) http://www.cyberciti.biz/pdf/lsst/ch02sec11.html (2 of 2) [7/29/2002 6:51:58 PM] Linux Shell Scripting Tutorial (LSST) v1.05r3 Prev Chapter 2: Getting started with Shell Programming Next More command on one command line Syntax: command1;command2 To run two command with one command line. Examples: $ date;who Will print today's date followed by users who are currently login. Note that You can't use $ date who for same purpose, you must put semicolon in between date and who command. Prev Home Next Wild cards (Filename Shorthand or meta Characters) Up Command Line Processing LSST v1.05r3 > Chapter 2 > More command on one command line http://www.cyberciti.biz/pdf/lsst/ch02sec12.html [7/29/2002 6:52:00 PM] Linux Shell Scripting Tutorial (LSST) v1.05r3 Prev Chapter 2: Getting started with Shell Programming Next Command Line Processing Try the following command (assumes that the file "grate_stories_of" is not exist on your system) $ ls grate_stories_of It will print message something like - grate_stories_of: No such file or directory. ls is the name of an actual command and shell executed this command when you type command at shell prompt. Now it creates one more question What are commands? What happened when you type $ ls grate_stories_of ? The first word on command line is, ls - is name of the command to be executed. Everything else on command line is taken as arguments to this command. For e.g. $ tail +10 myf Name of command is tail, and the arguments are +10 and myf. Exercise Try to determine command and arguments from following commands Answer: Command No. of argument to this command (i.e $#) Actual Argument ls 1 foo cp 2 y and y.bak mv 2 y.bak and y.okay tail 2 -10 and myf mail 1 raj sort 3 -r, -n, and myf date 0 clear 0 NOTE: $# holds number of arguments specified on command line. And $* or $@ refer to all arguments passed to LSST v1.05r3 > Chapter 2 > Command Line Processing http://www.cyberciti.biz/pdf/lsst/ch02sec13.html (1 of 2) [7/29/2002 6:52:02 PM] script. Prev Home Next More commands on one command line Up Why Command Line arguments required LSST v1.05r3 > Chapter 2 > Command Line Processing http://www.cyberciti.biz/pdf/lsst/ch02sec13.html (2 of 2) [7/29/2002 6:52:02 PM] Linux Shell Scripting Tutorial (LSST) v1.05r3 Prev Chapter 2: Getting started with Shell Programming Next Why Command Line arguments required Telling the command/utility which 1. option to use. 2. Informing the utility/command which file or group of files to process (reading/writing of files). Let's take rm command, which is used to remove file, but which file you want to remove and how you will tell this to rm command (even rm command don't ask you name of file that you would like to remove). So what we do is we write command as follows: $ rm {file-name} Here rm is command and filename is file which you would like to remove. This way you tail rm command which file you would like to remove. So we are doing one way communication with our command by specifying filename. Also you can pass command line arguments to your script to make it more users friendly. But how we access command line argument in our script. Lets take ls command $ Ls -a /* This command has 2 command line argument -a and /* is another. For shell script, $ myshell foo bar Shell Script name i.e. myshell First command line argument passed to myshell i.e. foo Second command line argument passed to myshell i.e. bar In shell if we wish to refer this command line argument we refer above as follows myshell it is $0 foo it is $1 bar it is $2 LSST v1.05r3 > Chapter 2 > Why Command Line arguments required http://www.cyberciti.biz/pdf/lsst/ch02sec14.html (1 of 3) [7/29/2002 6:52:05 PM] Here $# (built in shell variable ) will be 2 (Since foo and bar only two Arguments), Please note at a time such 9 arguments can be used from $1..$9, You can also refer all of them by using $* (which expand to `$1,$2...$9`). Note that $1..$9 i.e command line arguments to shell script is know as "positional parameters". Exercise Try to write following for commands Shell Script Name ($0), No. of Arguments (i.e. $#), And actual argument (i.e. $1,$2 etc) Answer Shell Script Name No. Of Arguments to script Actual Argument ($1,..$9) $0 $# $1 $2 $3 $4 $5 sum 2 11 20 math 3 4 - 7 d 0 bp 3 -5 myf +20 Ls 1 * cal 0 findBS 4 4 8 24 BIG Following script is used to print command ling argument and will show you how to access them: Run it as follows LSST v1.05r3 > Chapter 2 > Why Command Line arguments required http://www.cyberciti.biz/pdf/lsst/ch02sec14.html (2 of 3) [7/29/2002 6:52:05 PM] Set execute permission as follows: $ chmod 755 demo Run it & test it as follows: $ ./demo Hello World If test successful, copy script to your own bin directory (Install script for private use) $ cp demo ~/bin Check whether it is working or not (?) $ demo $ demo Hello World NOTE: After this, for any script you have to used above command, in sequence, I am not going to show you all of the above command(s) for rest of Tutorial. Also note that you can't assigne the new value to command line arguments i.e positional parameters. So following all statements in shell script are invalid: $1 = 5 $2 = "My Name" Prev Home Next Command Line Processing Up Redirection of Standard output/input i.e.Input - Output redirection LSST v1.05r3 > Chapter 2 > Why Command Line arguments required http://www.cyberciti.biz/pdf/lsst/ch02sec14.html (3 of 3) [7/29/2002 6:52:05 PM] Linux Shell Scripting Tutorial (LSST) v1.05r3 Prev Chapter 2: Getting started with Shell Programming Next Redirection of Standard output/input i.e. Input - Output redirection Mostly all commands give output on screen or take input from keyboard, but in Linux (and in other OSs also) it's possible to send output to file or to read input from file. For e.g. $ ls command gives output to screen; to send output to file of ls command give command $ ls > filename It means put output of ls command to filename. There are three main redirection symbols >,>>,< (1) > Redirector Symbol Syntax: Linux-command > filename To output Linux-commands result (output of command or shell script) to file. Note that if file already exist, it will be overwritten else new file is created. For e.g. To send output of ls command give $ ls > myfiles Now if 'myfiles' file exist in your current directory it will be overwritten without any type of warning. (2) >> Redirector Symbol Syntax: Linux-command >> filename To output Linux-commands result (output of command or shell script) to END of file. Note that if file exist , it will be opened and new information/data will be written to END of file, without losing previous information/data, And if file is not exist, then new file is created. For e.g. To send output of date command to already exist file give command $ date >> myfiles (3) < Redirector Symbol Syntax: Linux-command < filename To take input to Linux-command from file instead of key-board. For e.g. To take input for cat command give $ cat < myfiles Click here to learn more about I/O Redirection You can also use above redirectors simultaneously as follows Create text file sname as follows LSST v1.05r3 > Chapter 2 > Redirection of Standard output/input i.e. Input - Output redirection http://www.cyberciti.biz/pdf/lsst/ch02sec15.html (1 of 2) [7/29/2002 6:52:06 PM] $cat > sname vivek ashish zebra babu Press CTRL + D to save. Now issue following command. $ sort < sname > sorted_names $ cat sorted_names ashish babu vivek zebra In above example sort ($ sort < sname > sorted_names) command takes input from sname file and output of sort command (i.e. sorted names) is redirected to sorted_names file. Try one more example to clear your idea: $ tr "[a-z]" "[A-Z]" < sname > cap_names $ cat cap_names VIVEK ASHISH ZEBRA BABU tr command is used to translate all lower case characters to upper-case letters. It take input from sname file, and tr's output is redirected to cap_names file. Future Point : Try following command and find out most important point: $ sort > new_sorted_names < sname $ cat new_sorted_names Prev Home Next Why Command Line arguments required Up Pipe LSST v1.05r3 > Chapter 2 > Redirection of Standard output/input i.e. Input - Output redirection http://www.cyberciti.biz/pdf/lsst/ch02sec15.html (2 of 2) [7/29/2002 6:52:06 PM] Linux Shell Scripting Tutorial (LSST) v1.05r3 Prev Chapter 2: Getting started with Shell Programming Next Pipes A pipe is a way to connect the output of one program to the input of another program without any temporary file. Pipe Defined as: "A pipe is nothing but a temporary storage place where the output of one command is stored and then passed as the input for second command. Pipes are used to run more than two commands ( Multiple commands) from same command line." Syntax: command1 | command2 Examles: Command using Pipes Meaning or Use of Pipes $ ls | more Output of ls command is given as input to more command So that output is printed one screen full page at a time. $ who | sort Output of who command is given as input to sort command So that it will print sorted list of users $ who | sort > user_list Same as above except output of sort is send to (redirected) user_list file $ who | wc -l Output of who command is given as input to wc command So that it will print number of user who logon to system $ ls -l | wc -l Output of ls command is given as input to wc command So that it will print number of files in current directory. LSST v1.05r3 > Chapter 2 > Pipes http://www.cyberciti.biz/pdf/lsst/ch02sec16.html (1 of 2) [7/29/2002 6:52:08 PM] $ who | grep raju Output of who command is given as input to grep command So that it will print if particular user name if he is logon or nothing is printed (To see particular user is logon or not) Prev Home Next Redirection of Standard output/input i.e.Input - Output redirection Up Filter LSST v1.05r3 > Chapter 2 > Pipes http://www.cyberciti.biz/pdf/lsst/ch02sec16.html (2 of 2) [7/29/2002 6:52:08 PM] Linux Shell Scripting Tutorial (LSST) v1.05r3 Prev Chapter 2: Getting started with Shell Programming Next Filter If a Linux command accepts its input from the standard input and produces its output on standard output is know as a filter. A filter performs some kind of process on the input and gives output. For e.g.. Suppose you have file called 'hotel.txt' with 100 lines data, And from 'hotel.txt' you would like to print contains from line number 20 to line number 30 and store this result to file called 'hlist' then give command: $ tail +20 < hotel.txt | head -n30 >hlist Here head command is filter which takes its input from tail command (tail command start selecting from line number 20 of given file i.e. hotel.txt) and passes this lines as input to head, whose output is redirected to 'hlist' file. Consider one more following example $ sort < sname | uniq > u_sname Here uniq is filter which takes its input from sort command and passes this lines as input to uniq; Then uniqs output is redirected to "u_sname" file. Prev Home Next Pipes Up What is Processes LSST v1.05r3 > Chapter 2 > Filter http://www.cyberciti.biz/pdf/lsst/ch02sec17.html [7/29/2002 6:52:09 PM] Linux Shell Scripting Tutorial (LSST) v1.05r3 Prev Chapter 2: Getting started with Shell Programming Next What is Processes Process is kind of program or task carried out by your PC. For e.g. $ ls -lR ls command or a request to list files in a directory and all subdirectory in your current directory - It is a process. Process defined as: "A process is program (command given by user) to perform specific Job. In Linux when you start process, it gives a number to process (called PID or process-id), PID starts from 0 to 65535." Prev Home Next Filter Up Why Process required LSST v1.05r3 > Chapter 2 > What is Processes http://www.cyberciti.biz/pdf/lsst/ch02sec18.html [7/29/2002 6:52:11 PM] Linux Shell Scripting Tutorial (LSST) v1.05r3 Prev Chapter 2: Getting started with Shell Programming Next Why Process required As You know Linux is multi-user, multitasking Os. It means you can run more than two process simultaneously if you wish. For e.g. To find how many files do you have on your system you may give command like: $ ls / -R | wc -l This command will take lot of time to search all files on your system. So you can run such command in Background or simultaneously by giving command like $ ls / -R | wc -l & The ampersand (&) at the end of command tells shells start process (ls / -R | wc - l) and run it in background takes next command immediately. Process & PID defined as: "An instance of running command is called process and the number printed by shell is called process-id (PID), this PID can be use to refer specific running process." Prev Home Next What is Processes Up Linux Command(s) Related with Process LSST v1.05r3 > Chapter 2 > Why Process required http://www.cyberciti.biz/pdf/lsst/ch02sec19.html [7/29/2002 6:52:12 PM] Linux Shell Scripting Tutorial (LSST) v1.05r3 Prev Chapter 2: Getting started with Shell Programming Next Linux Command Related with Process Following tables most commonly used command(s) with process: For this purpose Use this Command Examples* To see currently running process ps $ ps To stop any process by PID i.e. to kill process kill {PID} $ kill 1012 To stop processes by name i.e. to kill process killall {Process-name} $ killall httpd To get information about all running process ps -ag $ ps -ag To stop all process except your shell kill 0 $ kill 0 For background processing (With &, use to put particular command and program in background) linux-command & $ ls / -R | wc -l & To display the owner of the processes along with the processes ps aux $ ps aux To see if a particular process is running or not. For this purpose you have to use ps command in combination with the grep command ps ax | grep process-U-want-to see For e.g. you want to see whether Apache web server process is running or not then give command $ ps ax | grep httpd To see currently running processes and other information like memory and CPU usage with real time updates. top See the output of top command. $ top Note that to exit from top command press q. To display a tree of processes pstree $ pstree * To run some of this command you need to be root or equivalnt user. NOTE that you can only kill process which are created by yourself. A Administrator can almost kill 95-98% process. But some process can not be killed, such as VDU Process. Exercise: You are working on your Linux workstation (might be learning LSST or some other work like sending mails, typing letter), while doing this work you have started to play MP3 files on your workstation. Regarding this situation, answer the following question: LSST v1.05r3 > Chapter 3 > Linux Command Related with Process http://www.cyberciti.biz/pdf/lsst/ch02sec20.html (1 of 2) [7/29/2002 6:52:14 PM] 1) Is it example of Multitasking? 2) How you will you find out the both running process (MP3 Playing & Letter typing)? 3) "Currently only two Process are running in your Linux/PC environment", Is it True or False?, And how you will verify this? 4) You don't want to listen music (MP3 Files) but want to continue with other work on PC, you will take any of the following action: 1. Turn off Speakers 2. Turn off Computer / Shutdown Linux Os 3. Kill the MP3 playing process 4. None of the above