Unix shell scripting tutorial file handling




















Search Forums. Show Threads. Show Posts. Shell Programming and Scripting. File handling, getopts command in unix. Registered User. Join Date: Jul I need to create a shell script having the menu with few options such as 1. Listing 2. Change permissions 3. Modify Contents 4. Delete Files 5. Exit 1. For 1. Listing: Display a special listing of files showing their date of modification and access time side by side along with their permissions, sizes and filenames.

The script should accept any number of arguments to display a selective listing and should abort if no filenames are specified as arguments. Provide headers for the columns and take care of all validations. Allows for placing file content inline in a script. Example: ex5 display , text 1:! It will also have variable 7: and command substitutions performed. Since the text string marking the end was quoted, it does not get variable and command subsitutions.

This short test script can be used to generate suitable output. The result is piped into other commands as needed. The output of the pipeline is redirected back to stderr, so that stdout and stderr of the script as a whole are what we expect. Again we first duplicate stdout to another file descriptor 3.

The inner subshell runs the first command, then writes the exit status to fd 4. The outer subshell redirects 4 to stdout so that it is captured by the backtics. Standard output from the first command inner subshell is passed into the pipeline as normal, but the final output of the pipeline is redirected to 3 so that it appears on the original stdout and is not captured by the backtics.

If any of the commands really care about inheriting open file descriptors that they don't need then a more correct command line closes the descriptors before running the commands.

Example: ex9 display , text A script wants to save a copy of all output in a file, but also wants a copy to the screen. This is equivalent to always running the script as script tee outfile 1:! It is faster to run a single process than to run a separate lookup for each user. Close the pipe to tell it we have finished. Arrays must be declared before use, e,g.

It also demonstrates use of getopt for options processing Example: getauthlogs display , text 1:! We could also grep CellServDB. I cop out and hard code one known DB server and get the others from it. Example: ex8 display , text 1:! This can be useful if the script has multiple possible exit points.

Don't use set-UID scripts. Most systems don't even allow a script to be made set-UID. It is impossible due to inherent race conditions to ensure that a set-uid script cannot be compromised. Use wrapper programs like sudo instead.

If possible, don't use temporary files. If the file already exists and you don't have permission to overwrite it, the script will fail.

If you do have permission to overwrite it, you will delete the previous contents. Safe scratch files can be made by creating a new directory, owned and writeable only by you, then creating files in there. Check exit status of everything you do. Don't trust user input contents of files data piped from other programs file names. One possible way to protect against weirdo characters in file names: A function to massage a list of filenames to protect weirdo characters e. Download a compressed tar file of all example scripts used in these notes.

Kill the command if it hasn't finished when the timeout expires. MailPkg display , text Tar, compress, split and uuendcode a set of files for mailing. Blinn Ptree original display , text Ptree ksh version display , text Runs "ps" to get a process listing and then reformats to show the process family hierarchies. The original example is pure Bourne shell and inefficient. The ksh version is a fairly simple translation to use ksh internal commands where possible, and avoid writing scratch files, and runs very much faster.

This entire tutorial was created from individual HTML pages using a content management system written as ksh scripts heavily using sed to edit the pages , coordinated by make. You can even write an entire web server as a shell script. Most of these commands will operate on a one or more named files, or will operate on a stream of data from standard input if no files are named. If the file does not exist, a new zero-byte file is created, which is often useful to signify that an event has occurred.

Think plumbing. To avoid these problems, ksh also provides the "print" command for output. Often awk is easier to use, even though it is a more complex program. The patterns can be simple fixed text, or very complex regular expressions. It has been around for more than two decades, owing its lingevity to the ruggedness built into it and its commands.

This book has two parts. Skip to content. Suppose we have to use arguments in a particular script then we just need to create a shell script starting from! The tip to use a shell scripting command is that it should be used where the action is redundant such that it can be automated as the main idea behind creating a shell script is to lessen the load of the end-user. Another trick would be to make use of Tab key while writing the command so as to complete the command. There are possibly endless commands in Unix for shell scripting and mastering all of them is not possible.

The focus should be on strengthening the basic commands and work with the rest as and when time permits. With daily practice, you would surely remember all these commands and a lot more. I hope you liked this article, Keep reading our articles.



0コメント

  • 1000 / 1000