13th Chapter Learning Shell Script

Source: Internet
Author: User
Tags date1

First Script
[email protected] script]# cat-N sh01.sh1#!/bin/Bash2#Program:3#This program Shows"Hello world!" inchyour screen. 4Path=/usr/local/java/jdk1.8.0_91/bin:/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/bin:/sbin:/root/bin:~/bin5export PATH; 6Echo-e"Hello world! \a \ n"     7Exit0

First line: #! /bin/bash declares the shell name used by this script

Line 23rd: Comments

Row four: The declaration of an environment variable so that the program executes some external commands without having to write an absolute path

Line five: Make the environment variable effective

Line Six: Main program section

Line seventh: Use the exit command to break the program and return a value to the system, after executing the script, use echo $? To get the value.

[email protected] script]# VI sh01.sh[[email protected] script]#. /Sh01.shhelloworld!  // View the value returned to the system by the script above [[email protected] script]# echo $? 0

a simple shell script exerciseInteractive Scripting: variable content is determined by the user
[email protected] script]# cat-N sh02.sh1#!/bin/Bash2Path=/usr/local/java/jdk1.8.0_91/bin:/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/bin:/sbin:/root/bin:~/bin3Export PATH4         5Read-p"Please input your first name:"FirstName #tell user to input their name6Read-p"Please input your last name:"LastName7Echo-e"\ Your Full name is: $firstname $lastname"[email protected] script]# sh sh02.shplease input your first name:wuplease input your last Name:chao your full name /c5> is: Wu Chao[[email protected] script]#
change with Date: Create a file with a date
//Viewing script Scripts[email protected] script]# cat sh03.sh#!/bin/Bashpath=/usr/local/java/jdk1.8.0_91/bin:/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/bin:/sbin:/root/bin:~/Binexport Pathecho-E"I'll use the ' touch ' command to create 3 files."Read-P"Please input your file name:"Fileuserfilename=${fileuser:-"filename"}date1=$ (date-d -2day +%y%m%d) Date2=$ (date-d -1day +%y%m%d) Date3=$ (Date +%y%m%d) File1=${filename}${date1}file2=${filename}${date2}file3=${filename}${date3}touch"$file 1"Touch"$file 2"Touch"$file 3"//Execute the Script[[email protected] script]# sh Sh03.shi would use'Touch'command to create3files.please Input your file Name:testfile[[email protected] script]#//View Results[Email protected] script]# ls-l testfile*-rw-r--r--.1Root root0July +  A: +testFile20160717-rw-r--r--.1Root root0July +  A: +testFile20160718-rw-r--r--.1Root root0July +  A: +Testfile20160719[[email protected] script]#

Note: $ () is the result of executing the code inside. ${} takes the value of the variable.

Numeric Operations
[email protected] script]# cat sh04.sh#!/bin/Bashpath=/usr/local/java/jdk1.8.0_91/bin:/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/bin:/sbin:/root/bin:~/Binexport Pathecho-E"Please input the numbers:"Read-P"First number :"N1read-P"Second Number:"N2total=$ (($n 1*$n 2)) Echo-E"\ n The result of $n 1 * $n 2 is ==> $total"[[ Email protected] script]# [[email protected] script]# sh sh04.shplease input ' numbers:first number: ASecond Number: atThe result of A* at  is==>276[email protected] script]#
var=$ (OP content)

Used to calculate

[[email protected] script]# echo $ ((3))

13th Chapter Learning Shell Script

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.