Linux OPS Practice-September 13, 2015-September 15 Course Assignments

Source: Internet
Author: User

1, describe the operation principle of shell program;

The shell is both the command language, the programming language, and the command interpreter.

Shell scripts are typically #! The starting text file, such as "#!/bin/bash", which is called shebang, specifies the path to interpret this script shell, which the system invokes when executing the script. The character # indicates the beginning of the comment. The comment section starts with #, continues to the end of the line, and the comment line is typically used to provide comment information for the code, or to pause execution of a line of code that is ignored by the interpreter.

The shell script is a stack of commands, the shell through lexical analysis, syntax analysis, semantic analysis, in order, select or loop to execute commands in the script. The shell process ends when the script runs

2, summarize all the knowledge points involved in shell programming (such as: variables, grammar, command status, etc., to take the picture of Yo);

variables, variable operations and condition testing

Conditional Judgment Statement If, case, and Read command

Loop Statement For,while,until

function, dead loop, module session programming

3, summarize the course of all the circular statement, the use of conditional judgment and related examples; (if (jpg|png is not exist); echo "You say a XX")

Loop Statement For,while,until

variables, variable operations and condition testing

4. Write a script: If a path does not exist, it is created as a directory; otherwise it is present and the content type is displayed; (Don't doubt it's that simple)

[email protected] ~]# cat test.sh

#!/bin/bash

#

Read-t 10-p "Please input a file path:" Path

If [-Z ' $path '];then

ECHO-E "\n\033[31merror:\033[0mplease input a file path"

Exit 1

elif [!-e "$path"];then

Mkdir-p $path $>/dev/null

echo "Your input $path is not exist"

Elif [-F "$path"];then

echo "$path is a general file"

Echo-e "\033[31mthe $path: \033[0m"

Cat $path

elif [-D "$path"];then

echo "$path is a directory"

Echo-e "\033[31mthe $path: \033[0m"

LS $path

else echo "$path unknown type"

Fi

Test results:

[Email protected] ~]# bash test.sh

Please input a file path:/etc/fstab

/etc/fstab is a general file

The/etc/fstab:

#

#/etc/fstab

# Created by Anaconda on Thu Sun 19 22:48:50 2015

#

# Accessible filesystems, by reference, is maintained under '/dev/disk '

# See mans Pages Fstab (5), Findfs (8), mount (8) and/or Blkid (8) for more info

#

/DEV/MAPPER/VOLGROUP-LV_ROOT/EXT4 Defaults 1 1

Uuid=46c42e9f-f101-4209-9590-afa295415eaf/boot EXT4 Defaults 1 2

/dev/mapper/volgroup-lv_data/data EXT4 Defaults 1 2

/dev/mapper/volgroup-lv_swap swap swap defaults 0 0

TMPFS/DEV/SHM TMPFS Defaults 0 0

Devpts/dev/pts devpts gid=5,mode=620 0 0

Sysfs/sys Sysfs Defaults 0 0

PROC/PROC proc Defaults 0 0

[Email protected] ~]# bash test.sh

Please input a file path:/tmp

/tmp is a directory

The/tmp:

Ks-script-utpps_ Yum.log

Ks-script-utpps_.log YUM_SAVE_TX-2015-09-15-20-24RCJDYN.YUMTX

Shell YUM_SAVE_TX-2015-09-16-20-29IHCHUJ.YUMTX

Test.sh YUM_SAVE_TX-2015-09-16-20-30ZOEQ7W.YUMTX

Test.sh YUM_SAVE_TX-2015-09-16-20-32ANADVE.YUMTX

[Email protected] ~]# bash test.sh

Please input a file path:xx

Your input xx is not exist

[[email protected] ~]# ls

~ CC Shell test3.sh xx

$ init.sh test1.sh Test3.sh.orig

AA Install.log Test1.sh.orig test.sh

Anaconda-ks.cfg Install.log.syslog test.sh Test.sh.orig

BB QQ Test.sh.orig trash.sh

[Email protected] ~]# bash test.sh

Please input a file path:/dev/null

/dev/null Unknown type

[Email protected] ~]# bash test.sh

Please input a file path:

Error:please Input a file path

[Email protected] ~]# bash test.sh

Please input a file PATH:AA bb cc

Your input AA bb cc is not exist

5, write a script to complete the following functions: Judging a given two values, which is the size of which small, given the number of methods: script parameters, command interaction (using read, still so simple)

[email protected] shell]# cat 6.sh

#!/bin/bash

#

Error () {

Cat<<eof

+--------------------------------------------------------+

| Function of the script is to compare, numbers size |

| Instructions:num_compare.sh NUM1 num2 |

| Example:num_compare.sh 11 22 |

+--------------------------------------------------------+

Eof

}

echo "$*" |grep ' [[: Alpha:] ' &>/dev/null && error && exit 1

If [$#-gt 2-o $#-le 1];then

Error

Exit 2

elif ["$"-gt "$"];then

Echo-e "The max is $1\nthe min is"

elif ["$"-lt "$"];then

Echo-e "The max is $2\nthe min is $"

elif ["$"-eq "$"];then

echo "and $ as large as"

else error

Fi

The test results are as follows:

[Email protected] shell]# bash 6.sh

+--------------------------------------------------------+

| Function of the script is to compare, numbers size |

| Instructions:num_compare.sh NUM1 num2 |

| Example:num_compare.sh 11 22 |

+--------------------------------------------------------+

[[email protected] shell]# bash 6.sh a 1

+--------------------------------------------------------+

| Function of the script is to compare, numbers size |

| Instructions:num_compare.sh NUM1 num2 |

| Example:num_compare.sh 11 22 |

+--------------------------------------------------------+

[[email protected] shell]# Bash 6.sh 1 A

+--------------------------------------------------------+

| Function of the script is to compare, numbers size |

| Instructions:num_compare.sh NUM1 num2 |

| Example:num_compare.sh 11 22 |

+--------------------------------------------------------+

[[email protected] shell]# Bash 6.sh XY ZX

+--------------------------------------------------------+

| Function of the script is to compare, numbers size |

| Instructions:num_compare.sh NUM1 num2 |

| Example:num_compare.sh 11 22 |

+--------------------------------------------------------+

[[email protected] shell]# Bash 6.sh 12a 2b3

+--------------------------------------------------------+

| Function of the script is to compare, numbers size |

| Instructions:num_compare.sh NUM1 num2 |

| Example:num_compare.sh 11 22 |

+--------------------------------------------------------+

[[email protected] shell]# Bash 6.sh 10 34

The Max is 34

The Min is 10

6, the sum of all the odd numbers within 100 (at least 3 methods. Yes, this is our assignment. ^_^)

(1), [[email protected] ~]# cat test4.sh

#!/bin/bash

#

Y=0

For x in $ (SEQ 1 2);d O # for loop

y=$[$y + $x]

Done

echo "The sum is $y"

[Email protected] ~]# bash test4.sh

The sum is 2500

(2), [[email protected] ~]# cat test5.sh

#!/bin/bash

#

X=1

While ["$x"-le);d o # while loop

If [$[$x%2]-eq 1];then

Let y+= $x

Fi

Let X + +

Done

echo "The sum is $y"

[Email protected] ~]# bash test5.sh

The sum is 2500

(3), [[email protected] ~]# cat test10.sh

#!/bin/bash

#

X=1

Until [$x-gt];d o # until cycle

If [$[$x%2]-eq 1];then

Let y+= $x

Fi

Let X + +

Done

echo "The sum is $y"

[Email protected] ~]# bash test10.sh

The sum is 2500

7, write a script to achieve the following functions:

(1) Pass two text file path to script;

(2) Show the number of blank lines in two files and their blank lines;

(3) Show the total number of files in two files and their total number of rows;

[email protected] ~]# cat test6.sh

#!/bin/bash

#

Read-t 10-p "Please enter the file path:" File1 file2 #发现一个问题, here I do not enter after 10S why does not end, press the ENTER key will not end

spaceline1=$ (grep "^$" $file 1|wc-l)

spaceline2=$ (grep "^$" $file 2|wc-l)

line1=$ (cat $file 1|wc-l)

line2=$ (cat $file 2|wc-l)

###############################################################

Compare () {# # Here we find that the 2nd and 3rd ask requirements can be implemented with an expression that is just a parameter, so here we define a function that can be implemented 2 times to save repeated commands

If [$a-gt $b];then

ECHO-E "Max $file 1\n $file 1: $a"

elif [$a-lt $b];then

ECHO-E "Max $file 2\n $file 2: $b"

elif [$a-eq $b];then

Echo-e "$file 1 and $file 2 as large as\n space line is $a"

else echo "Error Please enter the file path!"

Fi

}

##############################################################

A= $spaceline 1

b= $spaceline 2

echo "The Space_line:"

Compare

Echo

A= $line 1

b= $line 2

echo "The line:"

Compare

Test results:

[Email protected] ~]# bash test6.sh

Please enter the file path:/root/aa/root/bb

The Space_line:

Max/root/aa

/root/aa:2

The line:

Max/root/bb

/root/bb:11

[[email protected] ~]# cat AA

Aaaaaaaaaaaaaaaaaa

Aaaaaaaaaaaaaa

Aaaaaaaaaaaaaaaaaaa

[email protected] ~]# cat BB

bbbbbbbbbbbb

bbbbbbbbb

A

B

C

Ccc

Cccc

D

D

C

8. Write a script

(1) Prompt the user to enter a string;

(2) Judgment:

If the input is quit, exit the script;

Otherwise, (continue waiting for the user to enter and display its input string content);


[email protected] ~]# cat test8.sh

#!/bin/bash

#

Read-t 10-p "Please enter string:" var

Case $var in

Quit

Exit 1

;;

*)

echo "$var"

;;

Esac

Test results:

[Email protected] ~]# bash test8.sh

Please enter string:

[Email protected] ~]# bash test8.sh

Please enter STRING:NIHAODAF

Nihaodaf

[Email protected] ~]# bash test8.sh

Please enter String:quit

2.

[email protected] shell]# cat 9.sh

#!/bin/bash

#

While True;do

Read-p "Please enter string:" str

[$STR = = Quit]&&break

Echo $str

Done

Test results:

[Email protected] shell]# bash 9.sh

Please enter String:nihao

Nihao

Please enter STRING:SB

Sb

Please enter String:quit


This article is from the "Marco -51cto-jy1506401-12" blog, be sure to keep this source http://magedu.blog.51cto.com/4824230/1696592

Linux OPS Practice-September 13, 2015-September 15 Course Assignments

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.