Linux-practice (25-30)

Source: Internet
Author: User
Tags arithmetic

25, Scripting: Pass a string to the script, the script will treat the string as the user name, if the user does not exist, then add and set the same password as the user name

For:

#!/bin/bash

#

If ID $ &>/dev/null; Then

echo "is exist."

Else

Useradd $

echo "$" | passwd--stdin $ &>/dev/null

echo "Create $ successfully"

Fi


[Email protected] ~]# VI w25.sh

[Email protected] ~]#

[Email protected] ~]# chmod +x w25.sh

[Email protected] ~]#

[Email protected] ~]#/w25.sh Little

Little is exist.

[Email protected] ~]#


26. Script: Pass the path of two text files to the script as its parameter, if there is no file, end the script execution and report the error message, if all the files exist, compare the file name of the file with more rows in two files.

For:

#!/bin/bash

#

if! ID "$ && $" &>/dev/null; Then

echo "Error, the parameter is not exist."

Exit

Fi


line1=$ (cat $ | wc-l)

line2=$ (cat $ | wc-l)

If [$line 1-gt $line 2]; Then

echo "$"

Else

echo "$"

Fi



[Email protected] ~]# VI w26.sh

[Email protected] ~]#

[Email protected] ~]# chmod +x w26.sh

[Email protected] ~]#

[[email protected] ~]# ls

2.txt ace BCF cd6 t1file.txt??????

24p1.sh ACF BCG cdrom w25.sh??????

24p2.sh ACG BDE dest w26.sh??????

24p3.sh ade BDF Install.log Zhengshu??????

3.txt ADF BDG Install.log.syslog??????

789 ADG Boot.iso k?????????

8yu anaconda-ks.cfg C1 My ks.cfg??????

A123 BCE c78m m.z??????

[Email protected] ~]#

[Email protected] ~]#/w26.sh 2.txt 3.txt

Error, the parameter is not exist.

[Email protected] ~]#/w26.sh 24p1.sh 24p2.sh

Error, the parameter is not exist.


27. Scripting: Pass a path as a parameter to the script, and if the file exists, determine its file type.

For:

#!/bin/bash

#

If [-a]; Then

type1=$ (file $)

echo "$type 1"

Else

echo "is not exist."

Fi



[Email protected] ~]# VI w27.sh

[Email protected] ~]#

[Email protected] ~]# chmod +x w27.sh

[Email protected] ~]#

[Email protected] ~]#/w27.sh w26.sh

File w26.sh

[Email protected] ~]#./w27.sh 3.txt

File 3.txt

[Email protected] ~]# VI w27.sh

[Email protected] ~]#

[Email protected] ~]#./w27.sh 3.txt

3.txt:ascii text

[Email protected] ~]#


28. Scripting: Pass three integers to the script, asking:

1) If the user passes over not three parameters, report the correct usage;

2) If there are non-pure numeric strings in three parameters, report an error and prompt the user to enter a number;

3) Select the maximum and minimum numbers from the three integers and display them;

For:

#!/bin/bash

#

If [$#-ne 3]; Then

echo "Usage: $ (basename $) integer1 integer2 Integer3"

Exit

Fi


If echo "$ $" | grep "\<[^[:d igit:]]\+\>" &>/dev/null; Then

echo "Please input 3 integers."

Exit

Fi


if [$1-GT]; Then

if [$1-GT]; Then

echo "Max is $"

if [$2-GT]; Then

echo "Min is $"

Else

echo "Min is $"

Fi

Else

echo "Max is $"

echo "Min is $"

Fi

Exit

Fi


If [$2-gt $]; Then

if [$2-GT]; Then

echo "Max is $"

if [$1-GT]; Then

echo "Min is $"

Else

echo "Min is $"

Fi

Else

echo "Max is $"

echo "Min is $"

Fi

Exit

Fi


if [$3-GT]; Then

If [$3-gt $]; Then

echo "Max is $"

If [$2-gt $]; Then

echo "Min is $"

Else

echo "Min is $"

Fi

Else

echo "Max is $"

echo "Min is $"

Fi

Exit

Fi



[Email protected] ~]# VI w28.sh

[Email protected] ~]# chmod +x w28.sh

[Email protected] ~]#

[Email protected] ~]#./w28.sh

Usage:w28.sh Integer1 Integer2 Integer3

[Email protected] ~]#

[Email protected] ~]#/w28.sh q 23

Please input 3 integers.

[Email protected] ~]#

[Email protected] ~]#./w28.sh 12 13 14

Max is 14

Min is 12

Max is 14

Min is 12

[Email protected] ~]# VI w28.sh

[Email protected] ~]#

[Email protected] ~]#./w28.sh 12 13 14

Max is 14

Min is 12

[Email protected] ~]#


29, pass three parameters to the script, the first is an integer, the second is an arithmetic operator (including the basic addition, subtraction, multiplication, divide, modulo operator), the third is an integer, the calculation results are displayed;

1) determine whether the first and third parameters are integers;

2) Judge that the second arithmetic operator must be one of the given operators, or else an error;

3) If the division is not divisible, the two-bit precision is preserved.

For:

#!/bin/bash

#

If [$#-ne 3]; Then

echo "Please input 3 parameter"

Exit

Fi


If echo $ | grep "^\<[[:d igit:]]\+\>$" &>/dev/null; Then

If echo $ | grep "^\<[[:d igit:]]\+\>$" &>/dev/null; Then

If Echo | grep "+\|-\|*\|%" &>/dev/null; Then

Let Result=$[$1$2$3]

echo "$result"

Elif Echo | grep "\" &>/dev/null; Then

echo "Scale=2;$1/$3" | Bc

Else

echo "$ is Error"

Fi

Fi

Fi


[Email protected] ~]# VI w29.sh

[Email protected] ~]# chmod +x w29.sh

[Email protected] ~]#

[Email protected] ~]#./w29.sh

[Email protected] ~]#

[[email protected] ~]# echo $?

0

[Email protected] ~]#/w29.sh 2-1

1

[Email protected] ~]#/w29.sh 2/1

2.00

[Email protected] ~]#/w29.sh 10/5

2.00

[Email protected] ~]# VI w29.sh

[Email protected] ~]#

[Email protected] ~]#./w29.sh

./w29.sh:line 3:syntax error near unexpected token ' fi '

./w29.sh:line 3: ' fi [$#-ne 3]; Then

[Email protected] ~]# VI +3 w29.sh

[Email protected] ~]#

[Email protected] ~]#./w29.sh

Please input 3 parameter

[Email protected] ~]#


30. Ask for the sum of all even numbers within 100 and the sum of odd numbers.

For:

#!/bin/bash

#

Sum1=0

Sum2=0

For I in $ (SEQ 1 2 100); Do

sum1=$[$sum 1+ $i]

Done

echo "Simple sum is $sum 1"


For j in $ (Seq 2 2 100); Do

sum2=$[$sum $j]

Done

echo "Double sum is $sum 2"


[Email protected] ~]# VI w30.sh

[Email protected] ~]# chmod +x w30.sh

[Email protected] ~]#

[Email protected] ~]#./w30.sh

Simple sum is 2500

Double sum is 2550

[Email protected] ~]#


Linux-practice (25-30)

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.