linux[Basic]-15-Simple shell script [01]

Source: Internet
Author: User

Understanding shell Scripts

The shell script works in two ways:

Interactive: Enter a command that the shell interprets and executes a command

Batch: Write many commands to the shell script, and the shell executes all commands at once

Shell a script is a program that combines various commands into logical statements

To view all the shell interpreters available in the system:

[Email protected] ~]# Cat/etc/shells

/bin/sh

/bin/bash

/sbin/nologin

/usr/bin/sh

/usr/bin/bash

/usr/sbin/nologin

/bin/tcsh

/bin/csh

[Email protected] ~]#

To view the current shell interpreter:

[Email protected] ~]# echo "$SHELL"

/bin/bash

[Email protected] ~]#

Write a simple shell script

Shell scripts should include:

Script Declaration, comment information, executable statement

Script Declaration (#! ): Tell the system which shell to use to explain

Comment Information (#): Introduction to Features

Executable statement: Execute a specific command

Instance:

[Email protected] sh]# vim example.sh

#!/bin/bash

#For Example by Msl23

# blog:www.cnblogs.com/msl23

Pwd

Ll-a

There are three ways to execute shell scripts:

Script file path:./example.sh

SH script file path: SH example.sh

Source script file path: source example.sh

The Sh and source methods can execute the script directly, but the method of accessing the script path directly

The Execute permission is required to execute the script

[Email protected] sh]#./example.sh

-bash:./example.sh:permission denied

[Email protected] sh]# chmod u+x example.sh #给属主用户加执行的权限

[Email protected] sh]#./example.sh

/root/lab1/sh

Receive User's parameters

The following are the shell predefined variables that have been defined

$

Current Execution Shell program name of the script

$1-9,${10},${11} ...

Positional Variables for parameters

$#

How many parameters are there?

$*

Values for all variables

$?

whether the previous command succeeded, succeeded 0 , failure not 0

Instance:

[Email protected] sh]# vim example_1.sh

#!/bin/bash

#Test parameter passing

#Author: Msl23 blog:www.cnblogs.cn/msl23

echo " this shell the script name is: $ "

echo " there are $# . parameters, respectively: $* "

echo " First parameter: $ $, third parameter: $ "

[Email protected] sh]# chmod u+x example_1.sh

[Email protected] sh]#./example_1.sh One, three four five six

The shell script program name is:./example_1.sh

There are a total of 6 parameters, namely: One twothree four five six

First parameter: One , third parameter:three

linux[Basic]-15-Simple shell script [01]

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.