44f4994631ed663ccbb64d7a7c6919ca.ppt
- Количество слайдов: 47
Devi Ahilya Vishwavidyalaya Shell Scripting hmehta. scs@dauniv. ac. in School of Computer Science and Information Technology hmehta. scs@dauniv; ac. in
DAVV SCSIT Applications of Shell Scripts ³ Task 1: There is a text file having data in columns. Store a particular column in a different file. ³ Task 2: Sort a file. ³ Task 3: Apply sorting in the output file of Task 1. ³ Task 4: Store only unique data in the output file. hmehta. scs@dauniv. ac. in
SCSIT DAVV Applications of Shell Scripts ³ Task 5: Store the data which satisfy the given condition. ³ Task 6: Transfer the file to the client’s machine for the further processing. ³ Task 7: Determine whether the executed program completed successfully or not. ³ Task 8: In case of failure produce the signal for error. hmehta. scs@dauniv. ac. in
SCSIT DAVV Solution #!/bin/ksh HOST=‘ftp. myserverid. mydomain’ USER=‘My. Userid’ PASSWD=‘My. Password‘ FILE=“filename” OUTFILE=“newfile” MAILINGLIST=“supportmail. lst” LOG=“logfile” cut –c 5, 6 $FILE| sort| uniq > $OUTFILE awk '{if ($2 > 30) print $1}‘ $OUTFILE hmehta. scs@dauniv. ac. in
DAVV SCSIT Solution ftp -n $HOST > /tmp/ftp. worked 2> /tmp/ftp. failed <
DAVV SCSIT Applications of Shell Scripts ³ Requirement: Need to execute a program on a particular time (System time). ? hmehta. scs@dauniv. ac. in
DAVV SCSIT Applications of Shell Scripts ³ Requirement: Logging the information about the execution of a job. ? hmehta. scs@dauniv. ac. in
DAVV SCSIT Applications of Shell Scripts ³ Requirement: Need to execute a program on a particular time (System time) but after successful completion of a job. ? hmehta. scs@dauniv. ac. in
DAVV SCSIT Applications of Shell Scripts ³ Requirement: Need to execute a program on a particular time (System time) but after successful completion of a job. Also check for the existence of a particular file. ? hmehta. scs@dauniv. ac. in
DAVV SCSIT Applications of Shell Scripts ³ Requirement: Need to know that whether a particular job completed successfully or not. ? hmehta. scs@dauniv. ac. in
DAVV SCSIT Applications of Shell Scripts ³ Requirement: Sending Mail/ SMS in case of error in the execution. ? hmehta. scs@dauniv. ac. in
DAVV SCSIT Applications of Shell Scripts ³ Requirement: Working like database on the text files. ? hmehta. scs@dauniv. ac. in
DAVV SCSIT Applications of Shell Scripts ³ Requirement: Fetching columns/ rows from a file, counting the records, filtering the data, sorting the data, Pattern matching/ replacing. ? hmehta. scs@dauniv. ac. in
DAVV SCSIT Applications of Shell Scripts ³ Requirement: Handling CSV Files. ? hmehta. scs@dauniv. ac. in
DAVV SCSIT Applications of Shell Scripts ³ Requirement: Periodic Monitoring the system activities like disk space utilization etc. ? hmehta. scs@dauniv. ac. in
DAVV SCSIT Applications of Shell Scripts ³ Requirement: Sending / Receiving data to / from Remote location/computer. ? hmehta. scs@dauniv. ac. in
DAVV SCSIT Applications of Shell Scripts ³ Requirement: Checking for the existence and permission for a file. ? hmehta. scs@dauniv. ac. in
DAVV SCSIT Applications of Shell Scripts ³ Requirement: System administrators, for automating many aspects of computer maintenance, user account creation etc. ? hmehta. scs@dauniv. ac. in
DAVV SCSIT Applications of Shell Scripts ³ Requirement: Application package installation tools. ? hmehta. scs@dauniv. ac. in
DAVV SCSIT Applications of Shell Scripts ³ Requirement: Application startup scripts, especially unattended applications. ? hmehta. scs@dauniv. ac. in
DAVV SCSIT Applications of Shell Scripts ³ Requirement: Data Synchronization. ? hmehta. scs@dauniv. ac. in
DAVV SCSIT Applications of Shell Scripts ³ Requirement: Interface between Os and other tools/language like Java, Oracle, FTP. ? hmehta. scs@dauniv. ac. in
DAVV SCSIT Applications of Shell Scripts ³ Requirement: Log Rotation. ? hmehta. scs@dauniv. ac. in
DAVV SCSIT Applications of Shell Scripts ³ Requirement: Purging of old files and data. ? hmehta. scs@dauniv. ac. in
DAVV SCSIT Applications of Shell Scripts ³ Requirement: Removing blank files and File comparison. ? hmehta. scs@dauniv. ac. in
DAVV SCSIT The Shell & Shell Script ³ A shell is a command interpreter turns the input text in to actions. ±Bourne Shell ±Bourne Again Shell ±Korn Shell ±C Shell ±etc. . . . ³ A Shell Script is a logical sequence of commands. hmehta. scs@dauniv. ac. in
DAVV SCSIT The Anatomy of a Command ³ grep Command –i Option localhost /etc/hosts Arguments Options Change the behavior of a command Arguments control what the command act upon hmehta. scs@dauniv. ac. in
DAVV SCSIT Running the Shell Script ³ Type the name of a program and some command line options. ³ The shell reads this line, finds the program and runs it, feeding it the specified options. ³ The shell establishes 3 I/O channels: ² Standard Input ² Standard Output ² Standard Error hmehta. scs@dauniv. ac. in
DAVV SCSIT The Shebang (#!) ³ ³ The Shebang is a special comment. It specifies which shell to use to execute this shell script. If no “#!” found, the current shell will be used to run the script. Example #!/bin/ksh hmehta. scs@dauniv. ac. in
SCSIT DAVV Two ways to execute the Shell ³ Set the permission attributes as a executable file then execute it like a command. OR ³ Invoke the shell explicitly. sh backup 8 pm. sh hmehta. scs@dauniv. ac. in
SCSIT DAVV Debugging the Shell Script ³ Running a script in debug mode will print each line of shell script before it executes. ³ Enable debug mode after adding the –v after shell interpreter’s name in Shebang. hmehta. scs@dauniv. ac. in
SCSIT DAVV Advantages ³ Writing a shell script is much quicker than writing the equivalent code in other programming or scripting languages. ³ Shell scripts have no compilation step, so the script can be executed quickly while debugging. hmehta. scs@dauniv. ac. in
SCSIT DAVV Disadvantages ³ One significant disadvantage of using shell scripts is that they can run slowly due to the need to create potentially many new sub-processes for each of the many commands executed. ³ Simple sh scripts can be quite compatible with the extremely diverse range of Unix but more complex shell scripts can fail because of the many subtle differences between shells. hmehta. scs@dauniv. ac. in
DAVV SCSIT Programs and Standard I/O Standard Input (STDIN) Program Standard Output (STDOUT) Standard Error (STDERR) hmehta. scs@dauniv. ac. in
SCSIT DAVV Overwriting the Standard I/O Device ³ Input/ Output Redirection hmehta. scs@dauniv. ac. in
DAVV SCSIT Pipes • A pipe is a holder for a stream of data. • A pipe can be used to hold the output of one program and feed it to the input of another. prog 1 prog 2 STDOUT STDIN hmehta. scs@dauniv. ac. in
DAVV SCSIT Regular Expression ³ Regular Expressions provide a concise and flexible means for identifying text of interest. ³ Examples: ²[abc] matches a single a b or c ²[a-z] matches any of abcdef…xyz hmehta. scs@dauniv. ac. in
SCSIT DAVV Regular Expression ³ Examples: ². at matches any three-character string ending with "at", including "hat", "cat", and "bat". ² [hc]at matches "hat" and "cat". ² [^b]at matches all strings matched by. at except "bat". ² ^[hc]at matches "hat" and "cat", but only at the beginning of the string or line. ² [hc]at$ matches "hat" and "cat", but only at the end of the string or line. hmehta. scs@dauniv. ac. in
SCSIT DAVV Regular Expression ³ Examples: ². at matches any three-character string ending with "at", including "hat", "cat", and "bat". ² [hc]at matches "hat" and "cat". ² [^b]at matches all strings matched by. at except "bat". ² ^[hc]at matches "hat" and "cat", but only at the beginning of the string or line. ² [hc]at$ matches "hat" and "cat", but only at the end of the string or line. hmehta. scs@dauniv. ac. in
SCSIT DAVV Regular Expression Used by ²grep “Get Regular Expression and Print” – search files line by line ²sed Simple Editing tool, right from the command line ²awk Scripting language, executes “program” on matching lines hmehta. scs@dauniv. ac. in
SCSIT DAVV Important Commands (UNIX) ³ touch: create a new file / update timestamp of existing file. ³ grep: search for a specified string or pattern ³ chmod/ chown/ chgrp: Change permissions / ownership / group on a file ³ du/ df: Display hard disk information. ³ find: find a file ³ sort: sort a file into alphanumeric order (by lines. ) ³ sed: Invoke the stream editor. ³ tr: Translate characters. ³ awk: Invoke the awk scripting language. ³ split: Split up a file into smaller chunks. hmehta. scs@dauniv. ac. in
SCSIT DAVV Important Commands (UNIX) ³ ³ ³ ³ ³ at: Run a command / script at a specified time and date. Cut: cut specified field(s)/ character(s) from lines in file(s) more, less, and pg: page through a file head/ tail: display the start/ end of a file cmp: compare two files and list where differences occur (T/B) diff : compare the two files and display the differences (T) wc: display word (or character or line) count for file(s) mail/ mailx/ Mail: simple email utility available on Unix systems. paste: The paste command allows two files to be combined side-by-side. hmehta. scs@dauniv. ac. in
SCSIT DAVV Important Commands (Win. NT) ³ ³ ³ AT: Schedule a command to run at a later time ATTRIB: Change file attributes CACLS: Change file permissions. Clean. Mgr: Automated cleanup of Temp files, recycle bin COMP: Compare the contents of two files or sets of files FC: Compare two files FDISK: Disk Format and partition FIND: Search for a text string in a file Magnify: Display windows magnification MAPISEND: Send email from the command line MEM: Display memory usage hmehta. scs@dauniv. ac. in
SCSIT DAVV Important Commands (Win. NT) ³ ³ ³ ³ ³ MORE: Display output, one screen at a time MSG: Send a message NET: Manage network resources PERFMON: Performance Monitor QGREP: Search file(s) for lines that match a given pattern. SCHTASKS: Create or Edit Scheduled Tasks SCLIST: Display NT Services SORT: Sort input TOUCH: Change file timestamps USRSTAT List domain usernames and last login hmehta. scs@dauniv. ac. in
SCSIT DAVV Book for UNIX hmehta. scs@dauniv. ac. in
SCSIT DAVV Book for Win. NT hmehta. scs@dauniv. ac. in
Devi Ahilya Vishwavidyalaya Any Questions Thank You School of Computer Science and Information Technology hmehta. scs@dauniv; ac. in