Bash programming exercises and Answers

Source: Internet
Author: User

1. Delete the blank characters from the beginning of the/etc/grub.conf file

sed [email protected]^[[:space:]]*@@ '/etc/grub.conf

2. Replace the number in the "Id:3:initdefault:" line in the/etc/inittab file with 5

3. Delete blank lines in the/etc/inittab file

Sed/^$/d/etc/inittab

4. Delete the # number at the beginning of the/etc/inittab file

Sed ' s/^#*//g '/etc/inittab

5. Delete the # and the trailing whitespace characters from the beginning of a file, but require a white-space character after the # # number

Sed s/^#[[:space:]]*//g 123.txt

6. Delete white space characters at the beginning of the line of the # class followed by white space characters in a file and #

#abc

# Hello World

#hi World

Sed s/^[[:space:]]*#//g 123.txt

7. Take out a directory name for a file path

/etc/fstab

/var/log

Remove etc and Var

Directory Name:

echo "/etc/rc.d/" | Sed-r ' [email protected]^ (/.*/) [^/]+/[email protected]\[email protected] '

Base name:

echo "/etc/rc.d/" | Sed-r '[email protected]^/.*/([^/]+)/[email protected]\[email protected]'

Teacher Standard Answer:

Sed exercises:
1, delete the/etc/grub.conf file in the beginning of the blank character;
Sed-r ' [email protected]^[[:spapce:]][email protected]@g '/etc/grub.conf
2. Replace the number in the "Id:3:initdefault:" line in the/etc/inittab file with 5;
Sed ' [email protected]\ (id:\) [0-9]\ (: initdefault:\) @\15\[email protected] '/etc/inittab
3, delete the blank line in the/etc/inittab file;
Sed '/^$/d '/etc/inittab
4. Delete the # number at the beginning of the/etc/inittab file;
Sed ' [email protected]^#@@g '/etc/inittab
5, delete the beginning of a file # and the following white space characters, but requires the # number must be followed by a blank character;
Sed-r ' [email protected]^#[[:space:]][email protected]@g '/etc/inittab
6. Delete white space characters at the beginning of the line of the # class followed by white space characters in a file and #
Sed-r ' [Email protected]^[[:space:]]+#@@g '/etc/inittab
7. Take out a directory name for a file path;
echo "/etc/rc.d/" | Sed-r ' [email protected]^ (/.*/) [^/]+/[email protected]\[email protected] '
Base name:
echo "/etc/rc.d/" | Sed-r ' [Email protected]^/.*/([^/]+)/[email Protected]\[email protected] '

#abc
# Hello World
# Hi World

Practice:
Pass a user name parameter to the script to determine if the user's user name is consistent with the group name of their base group, and the results are displayed.
#!/bin/bash
#

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

echo "No such user"

Exit 10

Fi
if [' id-n u $ ' = = ' id-n-G $ ']; Then
echo "The Same"
Else
echo "Not the same"
Fi

=================================================================

Exercise: Write a script

Pass a parameter (single-character on the line) to the script, such as the parameter Q, quit, Q, quit, quit the script, otherwise, the user's parameters are displayed:

#!/bin/bash
#
if [$ = ' Q '];then
echo "Quitting"
Exit 0
elif [$ = ' Q '];then
echo "Quiting"
Exit 1
elif [$ = ' Quit ']; Then
echo "Quitting"
Exit 2
elif [$ = ' QUIT ']; Then
echo "Quiting"
Exit 3
Else
echo "$"
Fi

=======================================================================

Practice:
Pass three parameters to the script, the first is an integer, the second is an arithmetic operator, the third is an integer, the result of the calculation is displayed, and the two-bit precision is required to be preserved. Shaped like:
./calc.sh 5/2

Tips:

echo "SCALE=2;111/22;" | Bc

BC <<< "SCALE=2;111/22"

#!/bin/bash
#
A=$1
X=$2
B=$3
result= echo "scale=2; $A $x$b;" | Bc
Echo $result

====================================================================

Write a script:
Determine the current host CPU manufacturer whose information is in the Vendor ID row in the/proc/cpuinfo file.
If its manufacturer is AUTHENTICAMD, it will be shown as AMD;
If its manufacturer is Genuineintel, it will be displayed as Intel Corporation;
Otherwise, it is said that it is a non-mainstream company;

#!/bin/bash
#
mycpu= ' Cat/proc/cpuinfo | grep Vendor | Cut-d:-f2 | head-1 | Sed ' s/^[[:space:]]//g '
if [$MYCPU = "Genuineintel"]; Then
echo "INTEL chip Inside"
elif [$MYCPU = "Genuineintel"]; Then
echo "AMD chip Inside"
Else
echo "Unkown CPU Type"
Fi

=====================================================================

Write a script:
Pass three integers to the script to determine the maximum and minimum numbers and display them.
Max=0
Max-eq $
Max=$1
Max-lt
Max=$2

Using the 22 comparison method

#!/bin/bash
#
Max=0
Min=0
if [$1-lt]; Then
Max=$2
Min=$1
Else
Max=$1
Min=$2
Fi
If [$MAX-lt]; Then
Max=$3
Else
Min=$3
Fi
echo "Max number is $MAX, and the min number is $MIN"

======================================================================

Practice:
Pass 3 parameters to the script, the parameters are the user name. The user's account information is extracted and placed in the/tmp/testusers.txt file, and the beginning of each line is required to have a line number.

echo "1 $LINES" >>//tmp/testusers

echo "2 $LINES" >>/tmp/testusers

# # # #脚本有问题 # #

!/bin/bash
#
For I in ' seq 1 $# ';d o
#echo "' cat/etc/passwd | grep $I ' ">>/tmp/tempusers
echo $ ' $I '
Done

=======================================================================

Cycle Practice

Say hello to all users in the system:

lines= ' Wc-l/etc/passwd | Cut-d '-f1 '

For I in ' seq 1 $LINES ';d o echo "Hello, ' head-n $I/etc/passwd| Tail-1 | cut-d:-f1 ' ";d One

======================================================================

Write a script:
1, the value of the set variable file is/etc/passwd
2. Say hello to each user in/etc/passwd and show the shell of each other in turn, such as:
Hello, Root, your shell:/bin/bash
3, statistics a total number of users

Extension: Say hello to the default shell for bash users only

#!/bin/bash
#
file=/etc/passwd
Lines= ' Wc-l/etc/passwd| Cut-d '-f1 '
For I in ' seq 1 $LINES ';d o
echo "Hello, ' cat/etc/passwd | Head-n $I | Tail-1 | cut-d:-f1 ', Your shell is ' cat/etc/passwd| Head-n $I | Tail-1 | cut-d:-f7 ' "
Done

Extension: Say hello to the default shell for bash users only.

Extended version:

#!/bin/bash
#
Sum=0
file=/etc/passwd
Lines= ' Wc-l/etc/passwd| Cut-d '-f1 '
For I in ' seq 1 $LINES ';d o
User= ' cat/etc/passwd | Head-n $I | Tail-1 | cut-d:-f1 '
Ushell= ' cat/etc/passwd| Head-n $I | Tail-1 | cut-d:-f7 '
if [[$USHELL = = '/bin/bash ']]; Then #if $USHELL was null, then you'll get error with the sinlge[], so "use double[[]" when J Udge the string equation.
echo "Hello, $USER, Your shell is $USHELL"
sum=$[$SUM +1]
Fi
Done
echo "Total user is $SUM"

=======================================================================

Write a script:
1, add 10 users user1 to User10, password with the user name, but requires only the user does not exist in the case can be added;


#!/bin/bash
#
For I in ' seq 1 10 '; Do
if! ID user$i &>/dev/null; Then
Useradd user$i &>/dev/null
echo "User$i" | passwd--stdin user$i &>/dev/nul
Else
echo "User$i already existed, would not create"
Fi
Done

Extended:
Accept a parameter:
Add: Adding User user1. User10
Del: Delete User user1. User10
Other: Exit

#!/bin/bash
#
if [[$ = = Add]]; Then
For I in ' seq 1 10 '; Do
if! ID user$i &>/dev/null; Then
Useradd user$i &>/dev/null
echo "User$i" | passwd--stdin user$i &>/dev/nul
Else
echo "User$i already existed, would not create"
Fi
Done
elif [[= = = Del]];then
echo "Delete function is running"
For I in ' seq 1 ';d o
USERDEL-RF user$i &>/dev/null
Done
Fi

====================================================================

Write a script:
Calculates the number of positive integers within 100 that can be divisible by 3;
Modulus, residual:%
3%2=1
100%55=45

#!/bin/bash
#
Sum=0
For I in ' seq 1 ';d o
If [$ ($I%3)) = 0];then
sum=$[$SUM + $I]
Fi
Done
echo "The sum is $SUM"

====================================================================


Write a script:
Calculates the sum of all odd numbers and all the even numbers within 100, and displays them respectively;

#!/bin/bash
#
J=0
O=0
For I in ' seq 1 ';d o
If [$ ($I%2) = 0]; Then
j=$[$J + $I]
Else
o=$[$O + $I]
Fi
Done
echo "SUM J is $J"
echo "SU O is $O"

=====================================================================

Write a script that shows all the default shell-bash users and default shell-/sbin/nologin users on the current system, and counts the total number of users under each shell. The results appear as follows:
Bash,3users,they is:
Root,redhat,gentoo

Nologin, 2users, they is:
Bin,ftp

can be appended to a file, and then display that file

#!/bin/bash
#
Ushells= ""
Bash_count=0
Nologin_count=0
Lines= ' cat/etc/passwd | Wc-l '
For I in ' seq 1 $LINES ';d o
if [[' cat/etc/passwd | head-n $I | tail-1 | cut-d:-f7 ' = =/bin/bash]]; Then
echo "' cat/etc/passwd | Head-n $I | Tail-1 | cut-d:-f1 ' ">>/tmp/bash_users
bash_count=$[$bash _count+1]
elif [[' cat/etc/passwd | head-n $I | tail-1 | cut-d:-f7 ' = =/sbin/nologin]]; Then
echo "' cat/etc/passwd | Head-n $I | Tail-1 | cut-d:-f1 ' ">>/tmp/nologin_users
nologin=$[$nologin +1]
Fi
Done
echo "BASH, $bash _countusers, they is:"
Echo ' Cat/tmp/bash_users '
echo "Nologin, $nologin _countusers,they is:"
Echo ' Cat/tmp/nologin_users '

=======================================================================


This article is from the "Richier" blog, make sure to keep this source http://richier.blog.51cto.com/1447532/1632742

Bash programming exercises and Answers

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.