Linux Base shell script problem

Source: Internet
Author: User
Tags chmod egrep

1. Write Script/root/bin/sumid.sh, calculate the sum of the ID of the 10th user and the 20th user in the/etc/passwd file

Solution: vim/root/bin/sumid.sh

#!/bin/bash

Tenuser= ' cat/etc/passwd | Head-n10 | Tail-n1 |cut-d:-f3 '

Twentyuser= ' cat/etc/passwd | Head-n20 |tail-n1 | cut-d:-f3 '

Let Sum=$[tenuser+twentyuser]

echo "The Tenuser and Twentyuser finally is $sum"


2, write the script/root/bin/sumspace.sh, pass two file path as parameters to the script, calculate the sum of all the blank lines in the two files

Solution: vim/root/bin/sumspace.sh

#!/bin/bash

First= ' Cat $ | grep "^[[:space:]]*$" | Wc-l '

Second= ' Cat $ | grep "^[[:space:]]*$" | Wc-l '

Let Sum=$[first+second]

Echo $sum


3, scripting/root/bin/sumfile.sh, Statistics/etc,/var,/usr directory total number of sub-directories and files

Solution: vim/root/bin/sumfile.sh

#!/bin/bash

First= ' Ls-l/etc | Egrep "(^d.*) | (^-.*)" | Wc-l '

Second= ' Ls-l/var | Egrep "(^d.*) | (^-.*)" | Wc-l '

third= ' ls-l/usr | Egrep "(^d.*) | (^.*)" | Wc-l '

Let Sum=$[first+second+third]

Echo $sum


4, write the script/root/bin/argsnum.sh, accept a file path as a parameter, if the number of parameters is less than 1, the user is prompted "at least one parameter should be given", and immediately exit;


If the number of arguments is not less than 1, the number of blank lines in the file pointed to by the first parameter is displayed

Solution: vim/root/bin/argsnum.sh

#!/bin/bash

["$#"-lt 1] && echo "The Must one arg" && exit 20

[-E] | | {echo "No such file or directory" && exit 30;}

["$#"-ge 1] && echo "The Blankspace is ' grep" ^[[:space:]]*$ "$ | Wc-l ' "


5, write script/root/bin/hostping.sh, accept the IPV4 address of a host as parameter, test whether can connect. If it is able to ping, the user is prompted to "the IP address is accessible";


If the result is not ping, the user is prompted "The IP address is inaccessible"

Solution: [[! $ =~ ([0-9]|[ 1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]) \.) {3} ([0-9]| [1-9] [0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]]]


&& echo "Please print right IP format!" && exit 20

' Ping $1-c1 &>/dev/null ' && echo ' Yes ' | | echo "No"


6, write script/root/bin/checkdisk.sh, check the disk partition space and inode utilization, if more than 80%, the broadcast warning space will be full

Solution: vim/root/bin/checkdisk.sh

disk= ' Df-h | GREP/DEV/SD | Tr-s ""% | Cut-d%-f5 | Sort-nr | Head-n1 '

Inode= ' Df-i | GREP/DEV/SD | Tr-s ""% | Cut-d%-f5 | Sort-nr | Head-n1 '

["$disk"-GT] && echo "The disk is" $disk "% would man" | | echo the disk size is "$disk"%

["$inode"-GT 5] && echo the inode is ' $inode '% man | | {echo the Indoe size is "$inode" && exit 30;}


7, write script/bin/per.sh, judge the current user to the specified parameter file, whether unreadable and not writable

Solution: vim/bin/per.sh

#!/bin/bash

[!-r "[email protected]"] && [!-W "[email protected]"] && echo the "[email protected]" is not having W and r Permisson or have some one Permisson | | echo the "[email protected]"


Have W and R permission

#另一种写法

[! \ (-r "[email protected]"-A-W "[email protected]" \)] && echo the "[email protected]" is not having W and R P Ermisson or some one Permisson | | echo the "[email protected]"


Have W and R permission


8, write script/root/bin/excute.sh, determine whether the parameter file is the sh suffix of the ordinary file, if it is, add everyone can execute permissions, otherwise prompt the user non-script file

Solution: vim/root/bin/excute.sh

[["[Email protected]" =~ *\.sh]] && ' chmod a+x ' [email protected] ' | | echo the "[email protected]" is not a script file



9, write scripts/root/bin/nologin.sh and login.sh, implement prohibit and allow ordinary user login system

Solution: vim/root/bin/nologin.sh

#!/bin/bash

Touch/etc/nologin

echo "Disabled common user Login"


vim/root/bin/nologin.sh

#!/bin/bash

Rm-rf/etc/nologin

Prohibit normal user to execute the first script when logging in, allow to execute second script after login


10, write script/root/bin/agree.sh, prompt the user to enter a variety of yes or no can, enter the wrong words prompt "Please enter the correct option"

Solution: vim/root/bin/agree.sh

#!/bin/bash

Read-p "Do you agree,plase print Yes or No:" ANS (#变量名)

[["$ANS" =~ [yy]| [Yy] [Ee] [ss]| [Nn] [oo]| [Nn]] || {echo "Input right Options" && exit 20;}


11, let all users of the PATH environment variable value One more path, for example:/usr/local/apache/bin

Solution: Vim/etc/profile

Path= $PATH: $HOME/bin:/usr/local/apache/bin

Source/etc/profile


12. When the user root logs in, the command indicator turns red, and the following aliases are automatically enabled:

Rm= ' Rm–i '

cdnet= ' cd/etc/sysconfig/network-scripts/'

editnet= ' Vim/etc/sysconfig/network-scripts/ifcfg-eth0 '

Editnet= ' vim/etc/sysconfig/network-scripts/ifcfg-eno16777736 or Ifcfg-ens33 ' (if the system is CentOS7)

Solution: Vim ~/.BASHRC

Rm= ' Rm–i '

cdnet= ' cd/etc/sysconfig/network-scripts/'

editnet= ' Vim/etc/sysconfig/network-scripts/ifcfg-ens33 '

Export ps1= ' \[\e[1;31m\][\[email protected]\h \w]\$\[\e[0m\] '


SOURCE ~/.BASHRC


13, any user login system, display the warning message "hi,dangerous!" Red font ”

Solution: vim/etc/profile.d/danger.sh

#!/bin/bash

Echo-e "\033[1;5;31mhi,dangerous!\033[0m"


14, write script to generate basic format of script, including author, contact, version, time, description, etc.

Solution: vim/root/bin/createscript.sh

#!/bin/bash

Touch

chmod +x $

echo "#/bin/bash

#*************************************

#Filename: $

#Author: Fang

#Date: ' Date ' +%y-%m-%d%h:%m:%s "'

#************************************* ">

Vim + $


Linux Base shell script problem

Related Article

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.