A simple example of LinuxShell Programming

Source: Internet
Author: User
A simple example of LinuxShell programming-Linux general technology-Linux programming and kernel information. The following is a detailed description. Introduction: I have been learning shell for some time. I feel that learning shell is a multi-practice program like other languages. A lot of online shell beginners are worried that they will not be able to find the practice program. I have integrated some basic exercises I encountered during shell learning, hoping to inspire shell beginners. (Maybe it shouldn't be integration. I'm used to this word)

There is no order in the example: Each example may have multiple methods
1.
Search for all files containing codfei in/home/codfei and its subdirectories.

Method 1:
[Root @ localhost Linuxos] # grep-rsn "codfei"/home/
/Home/codfei/c/Unix_c/2: 1: codfei ::::::::::::::::::::::
/Home/codfei/c/Unix_c/1: 1: codfei ::::::::::::::::::::::
Binary file/home/codfei/c/. charset. c. swp matches

Method 2:
[Root @ localhost Unix_c] # find/home/codfei/-type f | while read I; do grep-n codfei $ I & echo -----; done
1: codfei ::::::::::::::::::::::
/Home/codfei/c/Unix_c/2
-----
1: codfei ::::::::::::::::::::::
/Home/codfei/c/Unix_c/1
-----
Binary file/home/codfei/c/. charset. c. swp matches
/Home/codfei/c/. charset. c. swp
-----

2. Design a Shell program and create 50 directories under the/userdata directory, that is, user1 ~ User50, and set the permission for each directory to rwxr-xr --

Method 1:

#! /Bin/bash

# The simplest and most efficient method

Mkdir-p/userdata/{1 .. 50} & chmod 754/userdata/{1 .. 50}


Method 2:

#! /Bin/bash

# Use the seq command to add the while read Structure

Seq 1 50 | while read I; do
Mkdir-p/userdata/$ I
Chmod 754/userdata/$ I
Done


Method 3:

Use for or while loop

#! /Bin/bash

I = 0
While [$ I-lt 50]; do
Let I = I + 1
Mkdir-p/userdata/$ I
Chmod 754/userdata/$ I
Done



#! /Bin/bash

For (I = 1; I <= 50; I ++); do
Mkdir-p/userdata/$ I
Chmod 754/userdata/$ I
Done

Method 4:

#! /Bin/sh


For D in user {1 .. 50}
Do
Mkdir-m 754-p $ D
Done

3.

There is a file named abc.txt in the linuxsystem. How can I append the current system time to the beginning of this file line?

Three methods:

Echo-e "'date' \ n' cat ABC.txt '"> ABC.txt
Echo "'date | cat-ABC.txt '"> ABC.txt
Sed-I "1i 'date'" ABC.txt
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.