My first bash program

I didn't realize that bash is so useful until recently. 

Just a minute before, I'm still a guy who just know some basic linux commands such as: cd, cp, pwd...

However, from now on, I'll begin to learn shell programming!

The problem I want to solve is:

I want to get the first 3 columns of a bunch of ".bed" format files.

How to solve it?

First cd to your target directory,

then type in the following commands

cut -f1,2,3 $old_filename > new_filename

however, if you have lots of files, you'll do this one by one, which is troublesome and time consuming!

So you have to learn to write bash programs!!!

Here's my first bash program to solve this problem:

for i in *.bed
do  
 cut -f1,2,3 $i > $(echo New_$i) 
done

*.bed:   represents all the files ends with .bed

do and done:   are necessary components of a for loop in bash

cut -f1,2,3 :   cut the first 3 columns of the file

> :   output the cut files to new ones

echo New_$i:   concatenate new name of the file

$:   dollar sign is reallly important. You cannot use 'i' solely to represent the object i. Instead, you have to use '$i' to represent the object!

原文地址:https://www.cnblogs.com/foreverycc/p/3092183.html