Monday, July 26, 2010

Linux Shell Scripting part 2

Wild cards * and ?

The character * is a wildcard, and matches against one or more character(s) in a file (or directory) name.

Examples:

ls * will show all the files inside the current directory

ls *.s will show only the files which has .s extension

ls a* will show only the files which begin with a

ls ab* will show only the files which begin with ab

ls ab*.c will show only the files which begin with ab and have the .c extension

The character ? is also a wild cars which matches exactly one character in a file(or directory)name.

Examples:

ls ? will show only the files where their file name is one character long

ls bo? Will show only the files where their file name is 3 character long and starts with bo

Command line arguments

Consider the following command

cp ~/abc ~/Desktop

In this command there are two arguments. What if someone try to run this command without any of those arguments? the shell will probably give an error indicating arguments are missing.This point is crucial when someone uses commands which necessarily need arguments in their shell script. In that case they should run their shell script provided with those arguments.

Example :

Suppose our shell script name is comLineArgs.

Refresh memory on how to run it.

./comLineArgs

If someone needs to give the inputs to their shell scripts externally they can give them as command line arguments.

./comLine Args aaa bbb

The command line arguments which are used at a time can be also be accessed inside the sell script simply as variables.

Shell script name can be accessed as $0

Suppose there is n number of arguments. They can be accessed as $1, $2, $3……. $n

If someone needs to find the number of arguments $# can be used.

$* represents all the arguments.

Example Shell script describing how to access command line arguments.

echo "Total number of command line argument are $#"
echo "$0 is script name"
echo "$1 is first argument"
echo "$2 is second argument"
echo "All of them are :- $* or $@"

Input output redirection

Redirecting the output

> symbol is used to redirect the output of a command.

Example:

cat > file1

The above command creates a new file file1 or overwrites the existing file file1. Cat command reads the inputs given by the keyboard and redirects its output which is normally printed on the screen into the file1.

>> symbol is used to redirect the output of a command as well.

Example:

cat >> file1

Same thing as the > symbol but won’t overwrite the file adds more lines to the end of the file.

Redirecting the input

sort command sorts the contents of list1. Note that here inputs for the sort command are provided by the list1 file.

Pipes

Pipe is a temporary storage that output of a one command is stored and passes to another command as its input.

Example:

ls -l | wc -l

Output of ls command is given as input to wc command So that it will print number of files in current directory.

1 comment :

  1. dongtam
    mu private
    tim phong tro
    http://nhatroso.com/
    nhac san cuc manh
    tổng đài tư vấn luật
    http://dichvu.tuvanphapluattructuyen.com/
    văn phòng luật
    tổng đài tư vấn luật
    dịch vụ thành lập công ty
    http://we-cooking.com/
    chém gió
    trung tâm ngoại ngữ
    Nhạc Thành trước hết chỉ dám tận lực luyện hóa một phân thân của Thái Dương Chân Hỏa mà thôi, hắn sợ thân thể của mình không chịu nổi, không ngờ phân thân thôi mà đã lợi hại như thế, nếu như không phải nhờ Kim Long thân thể thì hắn xem ra đã bị đốt cháy thành tro.

    Mặc dù Nhạc Thành biết rằng nếu như mình sử dụng Vô Thượng Chân Hỏa để che chở lục phủ ngũ tạng thì Thái Dương Chân Hỏa vô pháp tạo ra thương tổn lớn đối với mình nhưng như vậy thì không thể biến Thái Dương Chân Hỏa trở thành bổn nguyên của mình.

    Nhạc Thành cũng chỉ có thể căn răng chịu đựng mà thôi, nếu như muốn luyện hóa Thái Dương Chân Hỏa thành bổn nguyên hỏa thì chỉ có thể tiếp nhận sự thống khổ do Thái Dương Chân Hỏa gây nên.

    - Nhạc Thành, ngươi làm sao vậy?

    Nghe thấy tiếng kêu thảm thiết của Nhạc Thành, đám người Yến Hiểu Kỳ, Tôn Thi Thi Yêu Huyên liền cất tiếng hỏi.

    - Chủ nhân, người làm sao vậy?

    ReplyDelete