Home PC Games Linux Windows Database Network Programming Server Mobile  
           
  Home \ Programming \ Based shell: using read, the command-line script to achieve mass participation and input two integer calculation     - To install the latest version of the EPEL on CentOS 5.x or 6.x (Linux)

- How to install Eclipse Luna IDE on CentOS 7 / RHEL 7 (Linux)

- CV: Linux command displays the progress of the run command (Linux)

- Mounting kit under Fedora Linux (Linux)

- Linux install deploy Ansible (Linux)

- Let CentOS6 yum upgrade to support more source rpm package (Linux)

- Ubuntu installed racing game Speed Dreams 2.1 (Linux)

- Mumble installation source VoIP application on Ubuntu (Linux)

- To install the iNode client on UbuntuKylin 13.10 (Linux)

- Installed in the desktop version of Ubuntu Unity Tweak Tool (Linux)

- Java reflection summary (Programming)

- The new task parallel library feature in .NET 4.6 (Programming)

- Ubuntu security settings and tools (Linux)

- Ubuntu 14.04 Nvidia proprietary drivers for install two graphic cards (Linux)

- Use OpenSSL to generate a certificate detailed process (Linux)

- Ubuntu System Log Configuration / var / log / messages (Linux)

- MongoDB relations, references, index query coverage (Database)

- About MongoDB query method according to fuzzy field (Database)

- Linux VMware virtual machine after the cloning of the card can not start to solve (Linux)

- Share Practical Tutorial GitHub (Linux)

 
         
  Based shell: using read, the command-line script to achieve mass participation and input two integer calculation
     
  Add Date : 2016-05-02      
         
         
         
  Based shell Exercises: Using read interactive input, command-line scripting parameter passing two ways to achieve input two integer numbers and arithmetic calculation. Investigation shell basics include: variable definitions, read, if the judge sentences, regular expressions and other knowledge;

The first way: read the interactive input parameters

Thinking is: if the first two variables determine the input is empty, empty the prompts for two numbers; not empty it is determined whether the input is an integer, use expr, so that the role of the two variables are added, if the result 0 Description of digital input 2, as a result of non-0 non-integer instructions to enter, not prompted for a non-integer;

#! / Bin / bash
read -p "pls input two number:" a b
if [! -z $ b]
then
    expr $ a + $ b &> / dev / null
    if [$? -eq 0]
      then
          echo "a-b = $ (($ a- $ b))"
          echo "a + b = $ (($ a + $ b))"
          echo "a * b = $ (($ a * $ b))"
          echo "a / b = $ (($ a / $ b))"
        else
              echo "input is not zhengshu"
        fi
else
    echo "you must input two number"
fi

Execution results are as follows: Enter the letters being given, the input is not an integer; enter only one parameter will be given;

[linuxhost @ localhost ~] $ sh a.sh
pls input two number: 4 2
a-b = 2
a + b = 6
a * b = 8
a / b = 2
[linuxhost @ localhost ~] $ sh a.sh
pls input two number: a 2
input is not zhengshu
[linuxhost @ localhost ~] $ sh a.sh
pls input two number: 10
you must input two number

Script bug, if you enter three parameters, it will be given as follows:

[linuxhost @ localhost ~] $ sh a.sh

pls input two number: 1 3 3

a.sh: line 3: [: 3: binary operator expected

you must input two number

For the above script bug amended as follows:

Ideas for: more add a variable c, and if more than a judge, to determine whether $ a is empty, such as digital input 2 is empty prompt and exit; then determine whether the $ b is empty, if not empty prompts for two numbers and exit; only $ a $ b has an input value that is empty, and then determine whether the $ c is empty, if not empty execute the following judgment integer, such as $ c is not empty also prompted to enter two numbers and not to quit;

#! / Bin / bash
read -p "pls input two number:" a b c
if [-z $ a]
  then
 echo "you must input two number"
 exit
elif [-z $ b]
  then
 echo "you must input two number"
 exit
fi
if [-z $ c]
then
 expr $ a + $ b &> / dev / null
 if [$? -eq 0]
  then
  echo "a-b = $ (($ a- $ b))"
  echo "a + b = $ (($ a + $ b))"
  echo "a * b = $ (($ a * $ b))"
  echo "a / b = $ (($ a / $ b))"
 else
  echo "input is not zhengshu"
 fi
else
 echo "you must input two number"
fi

Execution results are as follows, nothing input, will be prompted to enter a character must enter two numbers; enter two values ​​with the letter prompted non-integer input;

[linuxhost @ localhost ~] $ sh a.sh
pls input two number:
you must input two number
[linuxhost @ localhost ~] $ sh a.sh
pls input two number: 1
you must input two number
[linuxhost @ localhost ~] $ sh a.sh
pls input two number: 1 a
input is not zhengshu
[linuxhost @ localhost ~] $ sh a.sh
pls input two number: 2 1
a-b = 1
a + b = 3
a * b = 2
a / b = 2

The second way: command line script parameter passing mode

Ideas for: ab defined two variables, accepts command-line arguments passed; $ # is the total number of input parameters; the number of parameters to judge entered not equal to 2, the issuer must enter two numbers; equal to 2, then the implementation of the following script;

[linuxhost @ localhost ~] $ cat b.sh
#! / Bin / bash
a = $ 1
b = $ 2
if [$ # -ne 2]
then
 echo "you must input two number"
 exit 1
else
 expr $ a + $ b &> / dev / null
 if [$? -eq 0]
  then
  echo "a-b = $ (($ a- $ b))"
  echo "a + b = $ (($ a + $ b))"
  echo "a * b = $ (($ a * $ b))"
  echo "a / b = $ (($ a / $ b))"
 else
  echo "input is not zhengshu"
  exit 1
 fi
fi

Execution results are as follows: the Senate is empty, for the three parameters will prompt you must enter two numbers; the Senate contains non-numeric prompts you entered is not an integer;

[linuxhost @ localhost ~] $ sh b.sh 3 a
input is not zhengshu
[linuxhost @ localhost ~] $ sh b.sh 3 2 3
you must input two number
[linuxhost @ localhost ~] $ sh b.sh 3 2
a-b = 1
a + b = 5
a * b = 6
a / b = 1

to sum up:

You can read and user interaction is good, the script is more complicated than that if judgment is not efficient;

Use the command line parameter passing judgment expression input parameters, efficiency and better understanding;
     
         
         
         
  More:      
 
- Five useful commands to manage file types and system time in linux (Linux)
- WebLogic administrator account and reset the password (Database)
- Explore Android SQLite3 multithreading (Programming)
- Linux System Getting Started Learning: hard disk partition, and to deal with traps (Linux)
- Spring use Cache (Programming)
- Linux using TCP-Wrapper Service Management (Linux)
- See how --nand flash timing diagram of a read operation Comments (Programming)
- Object Oriented Programming Java reflection (Programming)
- Oracle 11g on Linux system boot from the startup settings (Database)
- Bootstrap 3.3.5 release download, Web front-end UI framework (Linux)
- Xshell upload files to achieve Windows host Linux (Linux)
- CentOS 6.6 source compiler GCC upgrade to 4.8.2 (Linux)
- Deployment Kubernetes manage Docker example cluster on Ubuntu (Server)
- Install minimize RHEL / CentOS 7 things to do (Linux)
- Retro terminal in Linux (Linux)
- Eclipse 3.7.2 can not start solving under Ubuntu 14.04 (Linux)
- Linux Command study manual - GPG command (Linux)
- Hadoop new and old version of the difference in the size of the InputSplit (Server)
- Cacti Linux-based system monitoring and alarm (Linux)
- Use libpq under Ubuntu 14.04 (Linux)
     
           
     
  CopyRight 2002-2022 newfreesoft.com, All Rights Reserved.