background preloader

Heiner's SHELLdorado

Heiner's SHELLdorado

System Administration Toolkit: Build intelligent, unattended scripts System Administration Toolkit Martin BrownPublished on July 03, 2007 About this series The typical UNIX® administrator has a key range of utilities, tricks, and systems he or she uses regularly to aid in the process of administration. There are key utilities, command-line chains, and scripts that are used to simplify different processes. The unattended script problem There are many issues around executing unattended scripts—that is, scripts that you run either automatically through a service like cron or at commands. The default mode of cron and at commands, for example, is for the output of the script to be captured and then emailed to the user that ran the script. Therefore, you need better methods for trapping and identifying errors within the script, better methods for communicating problems, and optional successes to the appropriate person. Setting up the environment Before getting into the uses of unattended scripts, you need to make sure that you have set up your environment properly.

UNIX tips: Learn 10 good UNIX usage habits Break bad UNIX usage patterns Michael StutzPublished on December 12, 2006 When you use a system often, you tend to fall into set usage patterns. Sometimes, you do not start the habit of doing things in the best possible way. Sometimes, you even pick up bad practices that lead to clutter and clumsiness. One of the best ways to correct such inadequacies is to conscientiously pick up good habits that counteract them. Adopt 10 good habits Ten good habits to adopt are: Make directory trees in a single swipe Listing 1 illustrates one of the most common bad UNIX habits around: defining directory trees one at a time. Listing 1. It is so much quicker to use the -p option to mkdir and make all parent directories along with their children in a single command. Listing 2. You can use this option to make entire complex directory trees, which are great to use inside scripts; not just simple hierarchies. Listing 3. Change the path; do not move the archive Listing 4. Listing 5. Listing 6. Listing 7.

BASH, TCSH, etc. BashPitfalls - Greg's Wiki This page is a compilation of common mistakes made by bash users. Each example is flawed in some way. 1. for f in $(ls *.mp3) One of the most common mistakes BASH programmers make is to write a loop like this: for f in $(ls *.mp3); do # Wrong! some command $f # Wrong! Yes, it would be great if you could just treat the output of ls or find as a list of filenames and iterate over it. There are at least 6 problems with this: If a filename contains whitespace (or any character in the current value of $IFS), it undergoes WordSplitting. You can't simply double-quote the substitution either: for f in "$(ls *.mp3)"; do # Wrong! This causes the entire output of ls to be treated as a single word. Nor can you simply change IFS to a newline. Another variation on this theme is abusing word splitting and a for loop to (incorrectly) read lines of a file. IFS=$'\n' for line in $(cat file); do … # Wrong! This doesn't work! So, what's the right way to do it? for file in ./*.mp3; do # Better! 2. cp $file $target

Linux tip: Bash parameters and parameter expansions The bash shell is available on many Linux® and UNIX® systems today, and is a common default shell on Linux. In this tip you will learn how to handle parameters and options in your bash scripts and how to use the shell's parameter expansions to check or modify parameters. This article focuses on bash, and the examples were all run on Linux systems with bash as the shell. However, the same expansions are available in many other shells, such as ksh, ash, or dash, and you may use them with these shells on other UNIX systems or even environments such as Cygwin. This tip builds on the tools covered in the earlier tip Linux tip: Bash test and comparison functions. Some of the material in this article is excerpted from the developerWorks tutorial LPI exam 102 prep: Shells, scripting, programming, and compiling, which covers many basic scripting techniques. Passed parameters Inside a function or script, you can refer to the parameters using the bash special variables in Table 1. Listing 1. #! #! #!

Mastering the Bash History – Make Tech Easier The Bourne Again Shell, better known as bash, is the default for most Linux distributions. It’s extremely powerful as far as command shells go, and holds all kinds of nifty tricks for those willing to learn. One of the best features is, in my opinion, the command history system. There are many options to use with the bash history, some of them I use nearly every time I open a command shell. Up/Down Arrows Many of you probably know this one, but for anyone who doesn’t, it’ll make your life a lot easier. A similar feature to the one listed above is the double bang (!!). Bash will substitute the !! This also works further backward, you can do something like to recall the command 5 entries back in the history. ! Perhaps a few days ago you typed a long, complex command into your shell, for example a series of options for “wterm”. Be careful with this one. !? This is similar to the last feature in that it searches the history for the word entered, but unlike ! Ctrl-R Manual Search ! into your shell.

Linux tip: Bash test and comparison functions The Bash shell is available on many Linux® and UNIX® systems today, and is a common default shell on Linux. Bash includes powerful programming capabilities, including extensive functions for testing file types and attributes, as well as the arithmetic and string comparisons available in most programming languages. Understanding the various tests and knowing that the shell can also interpret some operators as shell metacharacters is an important step to becoming a power shell user. This article, excerpted from the developerWorks tutorial LPI exam 102 prep: Shells, scripting, programming, and compiling, shows you how to understand and use the test and comparison operations of the Bash shell. This tip explains the shell test and comparison functions and shows you how to add programming capability to the shell. Tests In any programming language, after you learn how to assign values to variables and pass parameters, you need to test those values and parameters. test and [ Listing 1. (( and [[

How To Look Like A UNIX Guru Terence Parr Last updated: August 30, 2006 Translations: UNIX is an extremely popular platform for deploying server software partly because of its security and stability, but also because it has a rich set of command line and scripting tools. Programmers use these tools for manipulating the file system, processing log files, and generally automating as much as possible. If you want to be a serious server developer, you will need to have a certain facility with a number of UNIX tools; about 15. This lecture takes you through the basic commands and then shows you how to combine them in simple patterns or idioms to provide sophisticated functionality like histogramming. [By the way, this page gets a lot of attention on the net and unfortunately I get mail from lots of people that have better solutions or stuff I should add. Everything is a stream The first thing you need to know is that UNIX is based upon the idea of a stream . $ ls > /dev/null # ignore output of ls $ ls -l | grep Aug | wc -l cd

Power Shell Usage: Bash Tips & Tricks Bash Tips & Tricks Simon Myers UKUUG Linux 2003 Conference • August 2003 This view lists all the slides from the talk in a continuous format. The slides are also designed to be viewed as a slideshow from this file with Mozilla or a compatible browser. 1 Intro Tips for driving Bash better in everyday Linux use Target audience: people who type things in Linux Assumptions: You suspect Bash has features useful to you You know that bash(1) is looong You’re too lazy to read it 2 Not a List of Keystrokes More interesting tips than just listing keystrokes This talk not about: Ctrl+W, Meta+BkSpc, Meta+D, Ctrl+K, Ctrl+U, Meta+F, Meta+B, Shift+Ins, Ctrl+T, Ctrl+], Ctrl+Meta+] If you want to learn keystrokes, look them up 3 History Command history means never having to retype previous commands In theory Default configuration makes this not always the practice Problems: Sometimes commands don’t seem to get saved It can be awkward to find those that have been 4 The ‘New Window’ Problem 6 ‘Loser Takes All’

Related: