2014 Horse linux0218-1 character test and generate script code for interactive options table

Source: Internet
Author: User

0218 lesson, first a simple small program, pay attention to the use of character testing, it is recommended that all the characters tested to be quoted, and in the character test matching mode (can also make regular expressions), the need to use double brackets, the topic is as follows:

Exercise: Determine if all users have a login shell

#!/bin/bash

For UserName in ' cut-d:-f1/etc/passwd ';d o

if [[' grep ' ^ $userName \> "/etc/passwd | cut-d:-f7 ' =~ sh$]]; Then

echo "Login User: $userName"

Else

echo "Nologin User: $userName"

Fi

Done

In the question, the more bizarre is that the first half of the decision can be referenced with the grep command, do not need to add double quotes, magic!



Exercise: Write a script

1, let the user interactively enter a user name, first determine whether the user exists, does not exist, then 7 for the exit code;

2, judge whether the user's shell is/bin/bash, if it is, it is displayed as "Bash User", the exit code is 0;

Otherwise, it is displayed as "not Bash User." And the exit code is 1.

Read-p "Please input usename:" username

if! grep "^ $username/>"/etc/passwd &>/dev/null;then

Exit 7

Elif

[[' grep ^ $username/>/etc/passwd |cut-d:-f7 ' =~/bin/shell]];then

echo "Bash user"

Exit 0

Else

echo "Not bash user"

Exit 1

The above question, taken from the Xueyou notes



Exercise: 1, display the following menu:

CPU) Show CPU Info

MEM) Show Memory info

Quit) quit

Enter your option:

2, if the user chooses the CPU, displays the file/proc/cpuinfo information

3, if the user selects men, then displays the information of the file/proc/meminfo

4, if the user chooses quit, then exits, and the exit code is 5;

5. If the user types other characters, the unknown option is displayed, please re-execute the script, exit code is 6.


The following answer is a simple version of the default user input lowercase option:

#!/bin/bash

Cat <<eof

Cpu:show CPU Info

Mem:show Memory Info

Quit:quit

Eof

Read-p "Enter your option:" Useroption

If ["$userOption" = = "CPU"] &>/dev/null;then

Cat/proc/cpuinfo

elif ["$userOption" = = "Mem"] &>/dev/null;then

Cat/proc/meminfo

elif ["$userOption" = = "Quit"] &>/dev/null;then

Exit 5

Else

echo "Bad input, try again." && Bash cpuinfo.sh

Fi


The following answer is the version that accepts the user-case command:

#!/bin/bash

Cat <<eof

Cpu:show CPU Info

Mem:show Memory Info

Quit:quit

Eof

Read-p "Enter your option:" Useroption

if [["$userOption" =~ [Cc][pp][uu]] &>/dev/null;then

Cat/proc/cpuinfo && Bash cpuinfo2.sh

elif [["$userOption" =~ [mm][ee][mm]] &>/dev/null;then

Cat/proc/meminfo && Bash cpuinfo2.sh

elif [["$userOption" = = [Qq][uu][ii][tt]]] &>/dev/null;then

Exit 5

Else

echo "Bad input, try again." && Bash cpuinfo2.sh

Fi

The last question is written by oneself, two versions, own run without error, hope that friends can debug.

After completing these, I met the legendary vim! The first is to learn how to quit vim, haha haha!

2014 Horse linux0218-1 character test and generate script code for interactive options table

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.