background preloader

Bash

Facebook Twitter

Tips

Xargs. Sharing. Moving to Freedom: Subversion (SVN) helper bash script to list repo dirs. Finding my way around SVN. So far I’ve created a repository to hold my WordPress theme files and miscellaneous scripts. The working directories are scattered around my home dir, and I found that I wanted a way to view the structure of the repo itself. You can do this with the svn list command: svn list --recursive And you’ll get a list of all your directories and files. Svn list -R | svn-dirs.sh -d 2 To produce the output: bash/ bash/nautilus-scripts/ home-bin/ web/ web/mtf/ (I also created a script that runs svn list on my local repo and pipes to svn-dirs.sh, so I don’t have to type in the URL every time.)

How does it work? First we use getopts to parse the command line options, which in this case is only -d, which lets us specify the max depth of directories we want to list. Next, we use read to read the output produced by svn list. Num_slashes=$(echo $inpt | tr -cd "/" | wc -c) And now, the script! Svn-dirs.sh script. 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. Passed parameters Part of the beauty of functions and shell scripts is the ability to make a single function or script behave differently by passing parameters to it. Inside a function or script, you can refer to the parameters using the bash special variables in Table 1.

Listing 1. Listing 2. [ian@pinguino ~]$ cat testfunc.sh #! #! #! Advanced Bash Shell Scripting Guide - Text Processing Commands. Sort File sorter, often used as a filter in a pipe. This command sorts a text stream or file forwards or backwards, or according to various keys or character positions. Using the -m option, it merges presorted input files.

The info page lists its many capabilities and options. See Example 10-9, Example 10-10, and Example A-8. tsort Topological sort, reading in pairs of whitespace-separated strings and sorting according to input patterns. uniq This filter removes duplicate lines from a sorted file. The useful -c option prefixes each line of the input file with its number of occurrences. The sort INPUTFILE | uniq -c | sort -nr command string produces a frequency of occurrence listing on the INPUTFILE file (the -nr options to sort cause a reverse numerical sort). Example 12-11. Expand, unexpand The expand filter converts tabs to spaces. The unexpand filter converts spaces to tabs. Cut A tool for extracting fields from files. Using cut to obtain a listing of the mounted filesystems: paste join head wc.

Four ways to extract the current directory name. By Sergio Gonzalez Duran on November 06, 2007 (9:00:00 AM) Using basename Using the basename command is the easiest and simplest way to extract the current directory: basename /usr/local/bin bin However, it isn't useful in a shell script with changing directory variables. You can combine it with pwd inside backticks to make it more dynamic: cd /usr/local/binbasename `pwd` bin Using parameter substitution with echo The bash scripting language is full of nice tricks, including parameter substitution, which allows you to manipulate or expand variables. Cd /var/log/squidecho ${PWD##*/} squid PWD is the environment variable that holds the current path, and ## is the instruction that tells the script to remove everything it finds up to */.

Using awk and rev A more elaborate solution uses a combination of awk (a pattern-scanning utility) and rev (a utility that reverses lines from a file or from stdin): cd /usr/share/cups/datapwd | rev | awk –F \/ '{print $1}' | rev data Using sed sed 's,^(.*/)? 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. There is a PDF version available, suitable for printing on A4 paper. 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 4 The ‘New Window’ Problem Opening a terminal might read 400 history lines off disk Type 100 new command lines into it One command is taking a while to finish… 5 The ‘New Window’ Problem So open a second window.

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 [[ Linux tip: Bash parameters and parameter expansions. BASH, TCSH, etc. Linux Shell Scripting Tutorial - A Beginner's handbook. Linux and general notes.

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. Some of these tools come with the operating system, but a majority of the tricks come through years of experience and a desire to ease the system administrator's life. The focus of this series is on getting the most from the available tools across a range of different UNIX environments, including methods of simplifying administration in a heterogeneous environment. 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.

Setting up the environment Some things you might need to think about include: Listing 1. Listing 2. Listing 3. IRC Shell Hosting Packages with support for Eggdrop, BNC and IRCD.