Shell Scripting

8 examples of Bash if statements to get you started

Shell scripting is a fundamental skill that every systems administrator should know. The ability to script mundane & repeatable tasks allows a sysadmin to perform these tasks quickly. These scripts can be used for anything from installing software, configuring software or quickly resolving a known issue. A fundamental core of any programming language is the if statement. In this article I am going to show several examples of using if statements and explain how they work.

5 Bash for loop examples to make command line tasks more efficient

One of the things that excited me while learning Unix/Linux was how quickly one can perform tasks via the command line. Bash is a fully functional scripting language that incorporates Variables, Loops and If/Then statements; the bash shell allows a user to use these functions while performing adhoc tasks via the command line. This is also true for the other common shells such as bourne, korn shell, and csh.

Reading files in reverse with tac

Today’s article is going to cover a command that falls into the “I don’t use this often, but when I do it’s awesome” category. The tac command is very similar to the cat command in that it is used to concatenate and print files. However there is one very large difference, the tac command does this in reverse, starting with the last line of the file and working its way up to the first line.

Grepping a file without using cat, and other grep tricks

The grep command is a command that most Linux users learn early on, and many times they learn to use it via pipes (stdin). Because of this some Linux users just assume that grep can only be used with stdin; it’s ok, I was one of those too! Before I continue with some grep tricks I want to clarify the basic grep usage. Stop Doing This: $ cat file.log | grep "something" something Do This More:

SSH: Disable Host Checking for Scripts & Automation

In the world of Cloud Servers and Virtual Machines scripting and automation are top priority for any sysadmin. Recently while I was creating a script that logged into another server via SSH to run arbitrary commands, I ran into a brick wall. $ ssh 192.168.0.169 The authenticity of host ‘192.168.0.169 (192.168.0.169)’ can’t be established. ECDSA key fingerprint is 74:39:3b:09:43:57:ea:fb:12:18:45:0e:c6:55:bf:58. Are you sure you want to continue connecting (yes/no)? To anyone who has used SSH long enough the above message should look familiar.

Unix Shell: The Art of I/O Redirection

One of the primary tricks in my sysadmin bag-o-tricks is Input/Output Redirection; I have found that while many people use Shell I/O Redirection throughout their day not everyone fully understands why and how it works. The Input and Output In the Unix environment there is always 3 streams open stdin, stdout, & stderr; these special streams are used for interacting with the user input and program output within the Unix/Linux shell environment.

bash: Field Separator Variable

Filename manipulation with sed, awk and cut

Working with files is one of the most common tasks for systems administrators; because of that there are numerous ways to work with files in the Linux/Unix environment. Here is 2 quick examples on how to rename files. Example: imadmac:testing madflojo$ ls -la total 32 drwxr-xr-x 6 madflojo wheel 204 Aug 4 20:11 . drwxrwxrwt 6 root wheel 204 Aug 4 20:10 .. -rw-r–r– 1 madflojo wheel 8 Aug 4 20:11 1.

Bash: Repeated tasks with seq and for loops

There comes a time where every sysadmin needs to execute the same task multiple times. Whether you need to create 12,000 2MB files, create multiple users, or simply delete more then 50k files at a time; for loops will save you time and typing. For the instances where you need to execute a for loop a specific amount of times you can use seq to your advantage. Today’s example will show you how to create multiple files.

Quickly creating a CSV file

I have found that a systems administrators job doesn’t simply involve managing services and ensuring they are running. Many times the job includes extracting data from files and putting them into a usable form. Today’s example of Shell-Fu is creating a CSV list of the users on the system generated by the /etc/passwd file. [[email protected] play]$ cat /etc/passwd | cut -d: -f1| perl -pi -e ’s/n/,/’ root,bin,daemon,adm,lp,sync,shutdown,halt,mail,uucp,operator,games,[Continued] If you wanted to save this to a file you could use > or depending on whether you want to overwrite pre-existing data or not.