In the following example we are going to show how to remove columns from CSV file based on the column number. Consider a following linux command separated file containing 10 columns:
$ cat temp.csv 
1,2,3,4,5,6,7,8,9,10
1,2,3,4,5,6,7,8,9,10
1,2,3,4,5,6,7,8,9,10
1,2,3,4,5,6,7,8,9,10
1,2,3,4,5,6,7,8,9,10
1,2,3,4,5,6,7,8,9,10
1,2,3,4,5,6,7,8,9,10

Probably the easiest way to count number of columns in CSV file using bash shell is simply count number of commas in a single row. In the following example the content of the file myfile.csv is:
$ cat myfile.csv 
1,2,3,4,5
a,b,c,d,e
a,b,c,d,e

To list all available that is installed packages for your GNU R installation, start GNU R:
$ R

R version 3.0.2 (2013-09-25) -- "Frisbee Sailing"
Copyright (C) 2013 The R Foundation for Statistical Computing
Platform: x86_64-redhat-linux-gnu (64-bit)

The Bash "Shellshock" bug is used to spread malware using botnets. To make sure that you can prevent your system against Shellshock exploit make sure that your system is up to date. Once you update your system use below vulnerability test to help you find out whether your system is vulnerable to Bash Shellshock attacks.
Simply open a terminal on your Linux system and execute the following linux command
$ env i='() { :;}; echo Your system is Shellshock vulnerable' bash -c "echo Shellshock Linux system vulnerability test"
If your system is vulnerable to the Bash "Shellshock" bug the above command will produce a following output:

How can I retrieve yesterday date using bash?
$ date '+%A, %Y-%m-%d'
Saturday, 2014-06-28
$ date -d "yesterday" '+%A, %Y-%m-%d'
Friday, 2014-06-27

If you are stubborn C programmer and wish to get your way when using BASH you will be happy to know that BASH offers C style syntax for writing for loops. Below you can find two examples of C style bash for loop:

Simple c-style bash for look with three iterations:
#!/bin/bash
MAX=3
for ((i=1; i <= MAX ; i++)) ; do
	echo "$i"
done

Although Bash scripting is not a full fledged compile-able programming language, it is still very powerful and the script size can grow to enormous size. Therefore, even when you are just occasional bash scripting user or complete beginner some techniques is good to know in order to help debug you bash code. In this article we list some most command and basic bash scripting debbuging techniques.

The first debugging technique is bu using -x bash option during script execution. This will print all lines of code that is execute. For an illustration let's consider a following example:
#!/bin/bash 

echo hello

myvar=3
echo $myvar
To debug this bash shell script use -x bash option during script execution:
$ bash -x bash-script.sh 
+ echo hello
hello
+ myvar=3
+ echo 3
3

After reading this short shell scripting tutorial you will be able to to create and execute your own bash shell script. Not previous knowledge of shell scripting is required. However, you are expected to have some knowledge of hot ot start terminal and how to edit text files with some text editor of your choice.

Writing your own first Shell Script

In this section and as well as a first step we are going to create a simple shell script doing nothing else but printing "Shell Scripting" on the terminal output. This will will your starting point. To your first basic script together you need to know two things:

This series of articles is dedicated to development on Linux systems. This tutorial focuses on C programming and covers such concepts as types, operators and variables, flow control, functions, pointers and arrays, structures, basic I/O, coding style and building a program as well as packaging for Debian and Fedora or getting a package in the official Debian repository.

Some Linux tools does not properly work with files which include spaces in their names. This simple bash for loop will remove white space from file names and rename/move for all files in the given directory.

Very often I need to upload some files to my web server from a command line. entering a user name and password, changing directory can be little tedious work. Here is a small bash script which make this work easier. This script first defines a variables such as hostname of the ftp server, username and password and then it creates ftp session and uploads file into your selected directory:

Here is a small bash script to test a hard drive transfer speed. It should be take as an approximation. The speed value is taken from Linux dd command output. One way to test your hard drive speed is to use hdparm command:

This is a simple skeleton of a bash countdown script. The script takes two arguments . Here are some examples of its usage:

  • countdown time to 90 minutes from now:
./bash-countdown.sh -m 90
  • countdown time to 23.3.2036 from now:
./bash-countdown.sh -d "Mar 23 2036"
  • countdown time to 21:06 from now:
./bash-countdown.sh -d 21:06
  • countdown time to 21:06:45 from now:
./bash-countdown.sh -d 21:06:45

This simple perl script will print your external IP address assigned by you internet provider ( great for someone with dynamic IP address ).

First, we download a html file from ipchicken.com and then phrase this file to match for IP address in format X.X.X.X using regular expression.

Next, the script removes all unnecessary characters and prints your external IP address on the terminal. Lastly, the script removes previously downloaded index.html file.

The following bash script allows user to check for domain name availability using a whois utility ( Domain hijacking made easy ). User needs to only supply single or multiple keywords and the script checks domain availability for each keyword+domain-label. See below for a sample output.

Please feel free to add more generic domain labels into the list. However, test your whois server on what it returns if domain is available as not all whois servers return the exact string. I have tested only domains which are currently in the bash script below. When you get a string which your whois server returns when domain name is available, add it to the script's grep line starting with prefix"|". The chances are that if you add new domain name label the script will just work, but if not you know what to do.

Insert following lines into a file called check-domain-availability.sh

FIND LATEST LINUX JOBS on LinuxCareers.com
Submit your RESUME, create a JOB ALERT or subscribe to RSS feed.
LINUX CAREER NEWSLETTER
Subscribe to NEWSLETTER and receive latest news, jobs, career advice and tutorials.
DO YOU NEED ADDITIONAL HELP?
Get extra help by visiting our LINUX FORUM or simply use comments below.

You may also be interested in: