Shell script entry notes

Source: Internet
Author: User
Getting started with shell script


Note:

1. execute commands from top to bottom and from left to right;
2. execute commands as mentioned in Chapter 5: multiple blank spaces between commands and options are ignored;
3. blank lines will also be ignored, and the blank space opened by the [Tab] button is also a Space key;
4. If you read an enter sign (CR), try to execute this line (or this string) command;
5. If there is too much content in a line to the queue, you can use \ [enter] to extend it to the next line;
6. "#" can be used as an annotation! All the materials added after # will be marked as comments and ignored!



The first shell script must be hello World. Haha


#!/bin/bashPATH=/bin/:/sbin/:/usr/sbin/:/usr/local/sbin/:/usr/local/sbin:~/binexport PATHecho -e  "hello world!  \n"exit 0       



[Email protected]: ~ /Shell_script_beginner $ Sh./sh01.sh
Hello world!



It is explained that the default shell of Ubuntu is Dash instead of Bash. If there is a problem during echo-e,-E will be printed.


Reprinted link: http://webnoties.blog.163.com/blog/static/1835251412013518362635/

Thank you very much for helping me solve the problem that the default shell is not bash.

Because the default SH of Ubuntu is connected to dash, and the dash and bash are incompatible, an error occurs.During execution, you can replace sh with the bash file name. Sh to execute. Successful. Dash is something. Check that it should also be a shell, which seems to be a lot of criticism from users.
By the way, modify sh to connect to bash by default:
Sudo dpkg-reconfigure dash
Select No.












#! /bin/bash# code writer : EOF# code date   : 2014.07.29 # code file   : sh02.sh# e-mail      : [email protected]# code purpose:#               This program would ask the user to input some varible's value#       and then print them out into the screen.PATH=/bin/:/sbin/:/usr/sbin/:/usr/local/sbin/:/usr/local/sbin:~/binexport PATHread -p "Please input your first name:" first_name #give the user a message whatshould be inputedread -p "Please input your second name" second_nameecho -e "\nYour full name is $first_name $second_name" #print out the user's name                                                                                                                                                                                                                                                                                                                                                                                                                                                               


[Email protected]: ~ /Shell_script_beginner $ Sh./sh02.sh
Please input your first name: Jason
Please input your second nameleaster

Your full name is Jason leaster



#! /bin/bash# code writer : EOF# code date   : 2014.07.29# code file   : sh04.sh# e-mail      : [email protected]# code purpose:#               This program was coded for demonstrating some#       mathmatical operations on varibles which in bash script#       If you find something wrong with my code, please touch me.#       Thank you!PATH=/bin/:/sbin/:/usr/sbin/:/usr/local/sbin/:/usr/local/sbin:~/binexport PATHecho -e "You SHOULD input 2 numbers ,I will cross them !\n"read -p "first number: " first_numread -p "second number: " second_numtotal=$(($first_num*second_num))echo -e "total is : $total"
[Email protected]: ~ /Shell_script_beginner $ Sh./sh04.sh
You shoshould input 2 numbers, I will cross them!

First number: 10
Second number: 25
Total is: 250


I am not afraid of shame. The above is written as $ (total), and Shi is almost tangled ....



#! /bin/bash# code writer : EOF# code date   : 2014.07.29 # code file   : sh05.sh# e-mail      : [email protected]# code purpose:#               This program was coded for a demo for command -- test#       If you find something wrong with my code, please touch me.#       Thank you!PATH=/bin/:/sbin/:/usr/sbin/:/usr/local/sbin/:/usr/local/sbin:~/binexport PATHecho -e "Please input a filename, I will check the filename's type and permission.\n\n"read -p "Input a filename : " filenametest -z $filename && echo "You must input a filename." && exit 0test ! -e $filename && echo "The filename '$filename' DO NOT exist" && exit 0test -f $filename && filetype="regulare file"test -d $filename && filetype="directory"test -r $filename && perm="readable"test -w $filename && perm="writable"test -x $filename && perm="excutable"echo "The filename: $filename is a $filetype"echo "And the permissions are: $perm"


[Email protected]: ~ /Shell_script_beginner $ Sh./sh05.sh
Please input a filename, I will check the filename's type and permission.


Input a filename: sh01.sh
The filename: sh01.sh is a regulare File
And the permissions are: writable



Delete an environment variable with the unset environment variable name


 




#! /bin/bash# code writer : EOF# code date   : 2014.07.29# code file   : sh06.sh# e-mail      : [email protected]# code purpose:#               This program was coded for a demo for [ A==B ]#       If you find something wrong with my code, please touch me.#       Thank you!PATH=/bin/:/sbin/:/usr/sbin/:/usr/local/sbin/:/usr/local/sbin:~/binexport PATHread -p "Please input (Y/N) : " yn[ "$yn" == "Y" -o "$yn" == "y" ] && echo "OK, continue" && exit 0[ "$yn" == "N" -o "$yn" == "n" ] && echo "NO,interrupt" && exit 0echo "I don't know what you choice is" && exit 0


[Email protected]: ~ /Shell_script_beginner $ Sh./sh06.sh
Please input (y/N): Y
OK, continue



#! /bin/bash# code writer : EOF# code date   : 2014.07.29 # code file   : sh07.sh# e-mail      : [email protected]# code purpose:#               This program was coded for a demo that user input a file name#       and some parameters, lately program process it and print out the file#       type and permission.#       If you find something wrong with my code, please touch me.#       Thank you!PATH=/bin/:/sbin/:/usr/sbin/:/usr/local/sbin/:/usr/local/sbin:~/binexport PATHecho "The script name is ==> $0"echo "Total parameter numbe is ==> $#"[ "$#" -lt 2 ] && echo "The number of parameter is less than 2. Stop here" && exit 0echo "Your whole parameter is ==> [email protected]"echo "1st parameter ==> $1"echo "2nd parameter ==> $2"


[Email protected]: ~ /Shell_script_beginner $ Sh./sh07.sh
The script name is ==>./sh07.sh
Total Parameter numbe is => 0
The number of parameter is less than 2. Stop here
[Email protected]: ~ /Shell_script_beginner $ Sh./sh07.sh Hello World
The script name is ==>./sh07.sh
Total Parameter numbe is => 2
Your whole parameter is ==> Hello World
1st parameter => hello
2nd parameter => world





#! /bin/bash# code writer : EOF# code date   : 2014.07.30 # code file   : sh08.sh# e-mail      : [email protected]# code purpose:#               This program was coded for a demo for command -- shift#       If you find something wrong with my code, please touch me.#       Thank you!PATH=/bin/:/sbin/:/usr/sbin/:/usr/local/sbin/:/usr/local/sbin:~/binexport PATHecho "Total parameter number is ==> $#"echo "Your whole parameter is ==> [email protected]"shiftecho "Total parameter number is ==> $#"echo "Your whole parameter is ==> [email protected]"shift 3echo "Total parameter number is ==> $#"echo "Your whole parameter is ==> [email protected]"



[Email protected]: ~ /Shell_script_beginner $ Sh./sh08.sh
Total Parameter number is => 0
Your whole parameter is =>
Total Parameter number is => 0
Your whole parameter is =>
Total Parameter number is => 0
Your whole parameter is =>
[Email protected]: ~ /Shell_script_beginner $ Sh./sh08.sh Hello World
Total Parameter number is => 2
Your whole parameter is ==> Hello World
Total Parameter number is => 1
Your whole parameter is => world
Total Parameter number is => 1
Your whole parameter is => world
[Email protected]: ~ /Shell_script_beginner $ Sh./sh08.sh Hello World Jason leaster tonight
Total Parameter number is => 5
Your whole parameter is ==> Hello World Jason leaster tonight
Total Parameter number is => 4
Your whole parameter is ==> world Jason leaster tonight
Total Parameter number is => 1
Your whole parameter is ==> tonight


Interestingly, if there is no "parameter" after the file name, the parameter number is 0. If the parameter is less than the shift distance (for example, shift 3 in the Code), the minimum number of parameters is 1, if the parameter is less than the shift distance






#! /bin/bash# code writer : EOF# code date   : 2014.07.30# code file   : sh08.sh# e-mail      : [email protected]# code purpose:#               This program was coded for a demo for "if[] ; then fi"#       If you find something wrong with my code, please touch me.#       Thank you!PATH=/bin/:/sbin/:/usr/sbin/:/usr/local/sbin/:/usr/local/sbin:~/binexport PATHread -p "Please input (Y/N) :" ynif [ "$yn" == "Y" -o "$yn" == "y" ]; then        echo "OK,continue"        exit 0fiif [ "$yn" == "N" -o "$yn" == "n" ]; then        echo "Oh,interrupt!"        exit 0fiecho "I don't know what your choice is" && exit 0



[Email protected]: ~ /Shell_script_beginner $ Sh./sh09.sh
Please input (y/N): Y
OK, continue
[Email protected]: ~ /Shell_script_beginner $ Sh./sh09.sh
Please input (y/N): n
Oh, interrupt!







#! /bin/bash# code writer : EOF# code date   : 2014.07.30# code file   : sh9.sh# e-mail      : [email protected]# code purpose:#               This program was coded for a demo for "if[] ; elif [] ;then else fi"#       If you find something wrong with my code, please touch me.#       Thank you!PATH=/bin/:/sbin/:/usr/sbin/:/usr/local/sbin/:/usr/local/sbin:~/binexport PATHread -p "Please input (Y/N) :" ynif [ "$yn" == "Y" -o "$yn" == "y" ]; then        echo "OK,continue"        exit 0elif [ "$yn" == "N" -o "$yn" == "n" ];then        echo "Oh,interrupt!"        exit 0else        echo "Are you kidding me? You don't know what means \"Input (Y/N)\" \n"fiecho "I don't know what your choice is" && exit 0

[Email protected]: ~ /Shell_script_beginner $ Sh./sh10.sh
Please input (y/N): hehe
Are you kidding me? You don't know what means "input (y/n)" \ n
I don't know what your choice is









#! /bin/bash# code writer : EOF# code date   : 2014.07.30# code file   : sh10.sh# e-mail      : [email protected]# code purpose:#               This program was coded for a demo for "if[] ; elif [] ;then else fi"#       If you find something wrong with my code, please touch me.#       Thank you!PATH=/bin/:/sbin/:/usr/sbin/:/usr/local/sbin/:/usr/local/sbin:~/binexport PATHecho "This program will print your selection !"case $1 in        "one")        echo "Your choice is ONE"        ;;        "two")        echo "Your choice is TWO"        ;;        *)        echo "42"        ;;esac


[Email protected]: ~ /Shell_script_beginner $ Sh./sh12.sh Hello World
This program will print your selection!
42



#! /bin/bash# code writer : EOF# code date   : 2014.07.30 # code file   : sh13.sh# e-mail      : [email protected]# code purpose:#               This program was coded for a demo for "function and while loop"#       If you find something wrong with my code, please touch me.#       Thank you!PATH=/bin/:/sbin/:/usr/sbin/:/usr/local/sbin/:/usr/local/sbin:~/binexport PATHfunction secret(){        echo "hello world!"}temp=10while [  $temp != 0 ]do        secret        temp=$(($temp-1))        echo "$temp"done

[Email protected]: ~ /Shell_script_beginner $ Sh./sh13.sh
Hello world!
9
Hello world!
8
Hello world!
7
Hello world!
6
Hello world!
5
Hello world!
4
Hello world!
3
Hello world!
2
Hello world!
1
Hello world!
0



In the end, sh-x displays the script content of do to the screen. This is a useful parameter, and-N does not display any information.

[Email protected]: ~ /Shell_script_beginner $ sh-X./sh13.sh
+ Path =/bin/:/sbin/:/usr/local/sbin:/home/liuzjian/bin
+ Export path
+ Temp = 2
+ '['2 '! = '0']'
+ Secret
+ Echo 'Hello world! '
Hello world!
+ Temp = 1
+ Echo 1
1
+ '['1 '! = '0']'
+ Secret
+ Echo 'Hello world! '
Hello world!
+ Temp = 0
+ Echo 0
0
+ '['0 '! = '0']'




When I went to Hengshan, I was in bailongtan (you should be, don't remember ...) Two children... What a wonderful age...


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.