Shell beginners-several simple shell scripts

Source: Internet
Author: User
Tags unpack
Example 1
#! /Bin/shcase ${1 # *. Tar.} In bz2) tar jxvf $1; GZ) tar zxvf $1; *) echo "Wrong file type" esac
$1 indicates the first command line parameter ${1 # *. Tar.} when running the script. It indicates that the first command line parameter is removed from *. Tar. For example, if you use./mytar. Sh bak.tar.gz to run the script, then the 1st character indicates bak.tar.gz, and ${1 ##*. Tar.} indicates GZ.

Example 2
#! /Bin/bash
Unpack = 1
If [$ {1 # *.} = bz2]; then
Temp =$ {1% .*}
If [$ {temp ##*.} = tar]; then
Tar jxvf $1
Unpack = $?
Echo this is a tar.bz2 package
Else
Bunzip2 $1
Unpack = $?
Echo this is a bz2 package
Fi
Fi

The red mark above ${1 ### *.} = bz2, which is followed by two ##. what does one and one point mean?

The second red mark is followed by two "1%. *" s after temp.

I only know that this script is used to extract files. You have to select different decompressing commands based on different suffix names.

I can hardly understand the two marked red places
Where: $? Indicates:
Indicates the return value of the previous command. Generally, if it is 0, the operation is correct. If it is not 0, the operation is incorrect.
Shell programming 1) Compile shell scripts to integrate files D1 and D2 into file D3. 2) write two shell scripts S1 and S2. S1 can start three processes named A, B, and C respectively. The code for each process is as follows: int main () {While (1) {}; return 0;} S2 can kill the three processes and requires that the execution of S2 cannot be specified manually.

1) Cat D1 D2> D3 # The premise is that a text file can also be joined.
2) s1.sh

#! /Bin/bash
/Path/&
/Path/B &
/Path/C &

S2.sh
#! /Bin/bash
Kill-9 $ (pgrep A) # Kill-9 indicates that the process is forcibly killed.
# Or kill-9 $ (PS aux | grep-w a | grep-V grep | awk '{print $2 }')
Kill-9 $ (pgrep B)
# Same as above
Kill-9 $ (pgrep C)
# Same as above

In addition, let's talk about the pgrep command.

Pgrep is a tool used to query processes by program names. It is generally used to determine whether a program is running. In server configuration and management, this tool is often used and simple and clear;

 

Usage:

 

# Ps parameter option program name

 

Common Parameters

 

-L list the program name and process ID;
-The ID of the start of the O process;
-N indicates the ID of the process to terminate;

 

Example:

 

[root@localhost ~]# pgrep -lo httpd
4557 httpd

[Root @ localhost ~] # Pgrep-ln httpd
4566 httpd

[Root @ localhost ~] # Pgrep-l httpd
4557 httpd
4560 httpd
4561 httpd
4562 httpd
4563 httpd
4564 httpd
4565 httpd
4566 httpd

[Root @ localhost ~] # Pgrep httpd
4557
4560
4561
4562
4563
4564
4565
4566

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.