Home PC Games Linux Windows Database Network Programming Server Mobile  
  Home \ Programming \ Shell Script: create a function and specify a directory for download     - The lambda expression Java8 (constructor references) (Programming)

- Install multiple Linux distributions and Fedora 21 first experience on the same hard disk (Linux)

- CentOS install SystemTap-2.6 (Linux)

- Java concurrent programming combat (using synchronized synchronization method) (Programming)

- Java Access Control (Programming)

- Nginx logging client ip (Server)

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

- CentOS / Debian configuration Gitlab 7.1x to build self Git repository (Linux)

- Under Linux using Magent + Memcached cache server cluster deployment (Server)

- Approach the next Linux shared interrupts (Linux)

- Use mysqldump MySQL database backup - Linux Shell Scripting (Database)

- Thinking in Java study notes - Generics (Programming)

- C # C ++ Java interface type conversion (Programming)

- Github inventory objects Algorithm (Linux)

- The difference between vi and nano (Linux)

- CentOS 6.x systems installation + NIC driver installation (Realtek PCIe GBE Family Controller for Linux) (Linux)

- Implement binary search algorithm in C language (Programming)

- MySQL query optimization: profile function (Database)

- Linux kernel TCP / IP parameters analysis and tuning (Linux)

- Installation and operation GAMIT software under Linux operating system (Linux)

  Shell Script: create a function and specify a directory for download
  Add Date : 2017-08-31      
  Write a Shell script:

1, create a function that can accept two parameters:

1) The first parameter is the URL, you can download the file; the second argument is a directory, that is, after downloading stored position;

2) If the user to the directory does not exist, the user is prompted whether to create; if you create will continue, otherwise, the function returns an error value to the calling script 51;

3) if the directory exists, the download file; download command after execution of the test file successfully downloaded or not; if successful, 0 is returned to the calling script, otherwise, it returns to the calling script 52;

#! / Bin / bash

#writen by mofansheng @ 2015-08-10

url = $ 1

dir = $ 2

download ()


        cd $ dir &> / dev / null

        if [$? -ne 0]


        read -p "$ dir No such file or directory, create now? (y / n)" answer

                if [ "$ answer" == "y"]; then

                mkdir -p $ dir

                cd $ dir

                wget $ url &> / dev / null

                        if [$ -ne 0?]; then

                        return "52"



                return "51"



        wget $ url &> / dev / null

                if [$ -ne 0?]; then

                return "52"




download $ url $ dir

echo $?

Analyzing a lot if a little confused;

Validation results:

Directory exists, it returns 0, download files to the existing directory;

[Root @ localhost ~] # sh 1.sh http://www.linuxxxx.com/index.php yong


[Root @ localhost ~] # ls yong /


Directory does not exist, asks if you want to create, choose n is not created, then return 51;

[Root @ localhost ~] # sh 1.sh http://www.linuxxxx.com/index.php fan

fan No such file or directory, create now? (y / n) n


Directory does not exist, it asks if you want to create, choose y create, and download the file to the newly created directory Lane;

[Root @ localhost ~] # sh 1.sh http://www.linuxxxx.com/index.php fan

fan No such file or directory, create now? (y / n) y


[Root @ localhost ~] # ls fan /


Download the file is unsuccessful, it returns 52;

[Root @ localhost ~] # sh 1.sh http://www.linuxxxx.com/xxxx.php

- Linux VMware virtual machine after the cloning of the card can not start to solve (Linux)
- How LUKS encrypted disk / partition to perform remote incremental backup (Linux)
- ARP spoofing prevent Linux environment LAN (Linux)
- Design and implementation of environment sniffer running under Linux (Linux)
- Linux command -nohup & (Linux)
- Linux common network tools: traceroute routing of scanned (Linux)
- Protect against network attacks using Linux system firewall (Linux)
- CentOS 6.5 minimal installation and configuration VMware tools (Linux)
- System with Windows Remote Desktop to connect Ubuntu 15.04 (Linux)
- Java Virtual Machine Basics (Programming)
- Linux md5sum verify file integrity (Linux)
- VMware Workstation virtual machine Ubuntu achieve shared with the host (Linux)
- Eight sorting algorithm implemented in Python (Programming)
- MySQL import large amounts of data (Database)
- Nmcli based network management command-line tool (Linux)
- How x2go set up Remote Desktop on Linux (Linux)
- Top command: the Task Manager under linux (Linux)
- Binary tree traversal recursive and non-recursive (cyclic) traversal achieve (Programming)
- CentOS 7 source code to compile and install PHP5.6 Nginx1.7.9 and MySQL (LNMP build environment) (Server)
- 25 Git Usage Tips (Linux)
  CopyRight 2002-2020 newfreesoft.com, All Rights Reserved.