13Shell Script-Write a simple script

Source: Internet
Author: User
Tags file permissions

1. There are two ways to summarize shell script commands: Interactive and batch processing.

Interactive (interrctive): The user executes each command as soon as it is entered.

Batch: A complete shell script is written by the user beforehand, and the shell executes many commands in the script at once.

The name of the shell script file can be arbitrary. However, in order to avoid being mistaken for an ordinary file, it is recommended to add the. sh suffix to indicate that it is a script file. 2. The first simple shell script
[[email protected] 0614]# VI example.sh#!/bin/bash#for example by rockpwdls-l# the first method of execution bash script file name and its parameters or SH script file name and its parameters [[email protected] 0614]# bash example.sh/home/hk/0614total 20-rw-r--r--. 1 root root June 17:32 Aaa.txt-rw-r--r--. 1 root root 0 June 16:17 Abc.txt-rw-r--r--. 1 root root 0 June 16:23 Bbb.txt-rw-r--r--. 1 root root 0 June 16:27 Ccc.txt-rw-r--r--. 1 root root June 17:51 Example.sh-rw-rw-r--. 1 HK HK 4731 June 09:10 Openman.txt-rw-rw-r--. 1 HK HK $ June 09:11 practice.txt# The second method of execution allows the shell file permissions to be added to the executable and then run directly [[email protected] 0614]# chmod u+x examp Le.sh[[email protected] 0614]#./example.sh/home/hk/0614total 20-rw-r--r--. 1 root root June 17:32 Aaa.txt-rw-r--r--. 1 root root 0 June 16:17 Abc.txt-rw-r--r--. 1 root root 0 June 16:23 Bbb.txt-rw-r--r--. 1 root root 0 June 16:27 Ccc.txt-rwxr--r--. 1 root root June 17:51 Example.sh-rw-rw-r--. 1 HK HK 4731 June 09:10 openman.txt-rw-Rw-r--. 1 HK HK $ June 09:11 practice.txt# The third method is the file name of the source script and its arguments, or. Script file name and its parameters [[[email protected] 0614]# source Example.sh/home/hk/0614total 20-rw-r--r--. 1 root root June 17:32 Aaa.txt-rw-r--r--. 1 root root 0 June 16:17 Abc.txt-rw-r--r--. 1 root root 0 June 16:23 Bbb.txt-rw-r--r--. 1 root root 0 June 16:27 Ccc.txt-rw-r--r--. 1 root root June 17:51 Example.sh-rw-rw-r--. 1 HK HK 4731 June 09:10 Openman.txt-rw-rw-r--. 1 HK HK $ June 09:11 Practice.txt
3. script files that can accept user parameters

The shell has built-in variables that can be used to accept parameters, and spaces can be used between variables.

$0      当前shell脚本程序的名称$#      总共有几个参数$*      所有位置的参数值$?      显示上一次命令执行的返回值$1      第一个位置的参数值$N      第N个位置的参数值
[[email protected] 0615]# cat example.sh#!/bin/bashecho "当前脚本名称为$0"echo "总共有$#个参数,分别是$*。"echo "第1个参数为$1, 第二个参数为$2。"[[email protected] 0615]# sh example.sh one two three four five six当前脚本名称为example.sh总共有6个参数,分别是one two three four five six。第1个参数为one, 第二个参数为two。

13Shell Script-Write a simple script

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.