---
title: "Linux programming"
author: "zerostar88"
date: "2010-08-17T09:41:47Z"
tags: ["F#", "Linux"]
categories: ["操作系统"]
slug: "linux-programming"
draft: false
lastmod: "2010-08-17T10:58:51Z"
id: 738887
url: "https://jeffyding.iteye.com/blog/738887"
---

From today, I will learn how to do using shell command in Linux.

 

1\. 定义变量

    格式： 变量名=值， Linux没有类型之说，另外注意=前后没有空格， e.g.

    car=Benz

 

2\. 引用变量

   $变量名，e.g. $car

   echo $car

 

3\. 函数

   格式： function 函数名() {      }, function可以省略, e.g. pt.sh

             ptdate(){

                  date

             }

             ptdate

 4. 函数调用

    # ./pt.sh

 

 5. 带参数的函数, $1, $2 ...来引用参数变量

            ptdate(){

                  echo $1

                  echo $2

             }

            ptdate $1 $2

 

    调用时# pt.sh Hello World

 

6\. 来一个复杂的函数，从ftp上下载一个文件， vi ftp.sh

path=users/jeffyd/temp/  
file=sample.ldif  
hostname=\*\**  
username=\*\**  
password=\*\**

down(){

    if \[ -n "$1" ]; then  
      if \[ -d "$1" ];then  
         path=$1  
      fi       
    else     
        echo "Please input file path."  
        exit 1   
    fi  
     
    if \[ -n "$2" ]; then  
        if \[ -f "$2" ]; then  
            file=$2  
        fi       
    else     
        echo "Please input your file name."  
        exit 1   
    fi  
     
    ftp -vn $hostname &lt;&lt;EOF  
    user $username $password  
     
    cd $path  
    binary   
    get $file  
    quit     
EOF  
}

down $1 $2