Script Exercises (II.)

Source: Internet
Author: User

Use the script to print the directory in the current directory, print a first-level directory;

#!/bin/bashfor f in ' ls ' do if [-D $f] then echo $f fidone

------------------------------------------------------------------------------------

Create a function that can accept two parameters:

1) The first parameter is a URL, you can download the file, the second parameter is a directory, that is, the location of the download after the save;

2) If the user-given directory does not exist, the user is prompted to create it, or if it is created, the function returns a 51 error value to the calling script;

3) If the given directory exists, then download the file; Download command after the execution of the test file download success or not; If successful, return 0 to the calling script, otherwise, return 52 to the calling script

#!/bin/bashif [!-D $]then echo "make directory" exit 51ficd $2wget $1n= ' echo $? ' If [$n-eq 0];then E XIT 0else Exit 52fi

---------------------------------------------------------------------------------------

Scripts can take parameters or without, parameters can have more than one, each parameter must be a directory, the script checks the number of parameters, if equal to 0, the current directory itself; otherwise, the subdirectories contained in each parameter are displayed?

#!/bin/bashif [$# = 0]thenls-ld ' pwd ' elsefor i in ' seq 1 $# ' doa= $iecho "ls ${!a}" ls-l ${!a} |grep ' ^d ' Donefi

----------------------------------------------------------------------------------

The

Prompts the user to enter the name of the network card, and then we use the script to output the IP. Seemingly simple, but need to consider a number of aspects, such as we entered the name of the network card does not match the specification, how to deal with. The name conforms to the specification, but there is no response to the NIC at all.

#!/bin/bashwhile :d o     read -p  "Please enter the network card name: "  e      e1= ' echo  ' $e  | sed  ' s/[-0-9]//g '      e2= ' echo   "$e"  | sed  ' s/[a-za-z]//g '      if [ -z  $e  ]      then        echo  "You didn't enter anything"          continue     elif [ -z  $e 1  ]     then        echo  " Do not enter a pure number in CentOS the NIC name is preceded by the ETH plus the number "        continue      elif [ -z  $e 2 ]     then         echo  "Do not enter the pure letter in CentOS, the NIC name is after the beginning of the ETH plus number"          continue     else        break     fidoneip ()  {         ifconfig | grep -A1  "$1 "  |tail  -1 | awk  ' {print $2} '  | awk -F  ': '   ' {print $2} '}myip= ' ip  $e ' if [ -z  $myip  ]then    echo  "Sorry, there is no this NIC. "else    echo " The IP address of your network card is $myip "fi

---------------------------------------------------------------------------------------

Write an interactive script that implements this requirement: After running the script without any options, the prompt will appear:

Please select a number:

1: Restart httpd service

2: Restart MYSQDL service

3: Restart VSFTPD service

Add option--HTTPD will restart the httpd service, plus--mysql will restart the mysqld service, plus--ftp will restart VSFTPD service

#!/bin/bashif [$#-eq 0];thenread-p "Please select a number: 1; Restart httpd Service 2: Restart MYSQLD service 3: Restart VSFTPD service:" Numcase $num in1)/etc/init.d/ht tpd restart;; 2)/etc/init.d/mysqld restart; 3)/etc/init.d/vsftpd restart; *) echo "pram error, please try it again";; esacelsecase $ in--httpd)/etc/init.d/httpd restart; --MYSQL)/etc/init.d/mysqld restart;; --FTP)/etc/init.d/vsftpd restart;; *) echo "pram error, please try it again";; ESACFI

--------------------------------------------------------------------------------

Write a script to determine if the Web service is turned on in your Linux server? (Listen to port 80) if it is turned on, please determine what the service is running, is it httpd or nginx or something else?

#!/bin/bash## This script was for Judge if Web service was On,and what service provides.## written by Louis at 2014/10/14 17 : 45if netstat-nlp|grep-v ' grep ' |grep-q ': 80 '; Then web_pid= ' netstat-nlp|grep ': ' |awk ' {print $NF} ' |awk-f/' {print '} ' web_ser= ' ps aux|grep-v ' grep ' |grep $w Eb_pid|awk ' {print $11} ' echo ' Web service is On,it's main progress is ' $web _ser ' "Else echo" Web service is off. " Fi

This is a better answer, I did not think of the PID to find the path of service, only use the

Web_ser=netstat-nlp|grep ': |awk-f/' {print $} '

Then output the service name;


Script exercises (ii)

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.