Getting started with Shell programming (version 2) (I)

Source: Internet
Author: User
Tags echo command

Getting started with Shell programming (version 2) (I)
Simple example Shell program

Example 1.

#!/bin/bash#This is to show what a shell script looks likeecho "Our first example"echo # This inserts an empty line in output.echo "We are currently in the following directory."/bin/pwdechoecho "This directory contains the following files"/bin/ls -l .

Example 2.

#!/bin/bash# Auto mail for system info/bin/date +%F >> /tmp/sysinfoecho >> /tmp/sysinfoecho "Disk info:" >> /tmp/sysinfo/bin/df -h >> /tmp/sysinfoecho >> /tmp/sysinfoecho "Online users:" >> /tmp/sysinfo/usr/bin/who | /bin/grep -v root >> /tmp/sysinfoecho >> /tmp/sysinfoecho "Memory info:" >> /tmp/sysinfo/usr/bin/free -m >> /tmp/sysinfoecho >> /tmp/sysinfo# Write to root /usr/bin/write root < /tmp/sysinfo && /bin/rm -f /tmp/sysinfo# crontab -e# 0 9 * * 1-5 ./sysinfo.sh


Shell Structure

1 ,#! Specifies the Shell for executing the script

2. # comment the line to describe the role and owner of the script.

3. command and control structure

Procedure for creating a shell program

Step 1: create a file that contains commands and control structures.

Step 2: Modify the permission of this file so that it can execute: chmod u + x

Step 3: Execute./example or sh example or. sysinfo. sh

Shell variable

Variable: a method used by shell to transmit data. It represents the symbolic name of each value.

Shell has two types of variables: temporary variables and permanent variables.

Temporary variables are defined within the shell program. The scope of use is limited to the program that defines them and are invisible to other programs. Including User-Defined variables and location variables.

A permanent variable is an environment variable and its value does not disappear with the execution of the shell script.

1. User-Defined variables

A user-defined variable starts with a letter or underscore and is a string of letters, numbers, or underscores (_). It is case sensitive. Variable name length is not limited. When using the variable value, you must add the prefix "$" before the variable name ".

2. Set and use variables

Set variables: Typically, variables are named with uppercase letters. The variable name must start with an English letter and cannot contain numbers.

Variable Value assignment: there is no space on both sides of "=.

Assign a value when defining, for example, NUM = 1

Assign the execution result of a command to a variable, for example, TIME = 'date'; TIME = $ (date + % F)

Assign the value of A variable to another variable, for example, B = 120; A = $ B

Use the echo command to view the variable value. Example: echo $

3. variables that contain multiple words:

NAME = Mike Ron # An error occurred while running, which should be changed:

NAME = "Mike Ron" or $ NAME = 'Mike Ron'

4. Differences between single quotes and double quotes

For example, define DATE =$ (date + % F)

Time = "time is $ DATE"

Echo $ time

Time = 'Time is $ date'

Echo $ time

The content between single quotes is invariably specified to the variable.

5. set to view all variables defined in the system

Unset: deletes the defined variable.

6. Location Variables

Shell interprets the first part of the command line as the command name and the other part as the parameter when executing the USER command. The parameter determined by the location on the command line is called the location parameter.

For example:

Ls-l file1 file2 file3

$0 name of the program ls-l

$ N the nth parameter value of this program, n = 1 ~ 9

7. Special Variables

$ * All parameters of this program

$ # Number of parameters of this program

$ PID of the program

$! PID used to execute the previous background command

$? The Return Value of the previous command.

 

Example-autobak. sh

#!/bin/bash# backup files by dateDATE=$(/bin/date +%Y%m%d)/bin/tar -cf /backup/$1.$DATE.tar $1 > /dev/null 2>> /backup/$1.bak.log/bin/gzip  /backup/$1.$DATE.tarif [ $? -eq 0 ] then    echo "$1 $DATE backup successfully" >> /backup/$1.bak.logelse    echo "ERROR: failure $1 $DATE backup" >> /bakup/$1.bak.logfi# crontable -e# 0 3 * * 2,5 script 

Example-special. sh

#!/bin/bash# A test script for special varecho '$# is' $#echo '$* is' $*echo '$0 is' $0echo '$? is' $?echo '$$ is' $$echo '$2 is' $2
Shell commands

1. read Command [interaction mode]

Read USERNAME

Example-read. sh

#!/bin/bash# A test script for readread first second thirdecho "Your first parameter is $first"echo "Your second parameter is $second"echo "Your third parameter is $third"
# Sh-x command: single-step debugging display and execution

2. expr command to calculate Integer Variables

For example, there must be a space between expr 3 + 5 # "+"

Expr $ var1-5

Expr $ var1/$ var2

Expr $ var3 \ * 10 # "*" requires an escape character "\"

Complex operations:

Expr 'expr 5 + 7'/$ var4

Assign the calculation result to the variable:

Var4 = 'expr $ var1/$ var2'

Example-expr. sh

#!/bin/bash# A test for expra=10b=20c=30val1=$(expr $a + $b + $c)echo "The value of val1 is $val1"val2=$(expr $c / $b)echo "The value of val2 is $val2"val3=`expr $a \* $b`echo "The value of val3 is $val3"val4=`expr $a + $c / $b`echo 'The value of $a + $c / $b is ' $val4

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.