您的位置:首页 > 运维架构 > Shell

初学shell脚本编程注意空格问题

2014-04-24 14:59 519 查看

一、什么是shell编程

shell编程就是利用shell的功能所写的一个程序,这个程序是使用纯文本文件,将一些shell的指令写在里面,然后用正规表示法,管道命令以及数据流重导向等功能,以达到我们所想要的处理目的。我们使用到shell有bash,sh,csh,ksh常用的是bash。

二、shell编程要注意到空格问题

我们举个例子来说吧
#!/bin/bash
function show_usag {    #show_usag 应该与"{" 分开

echo "Usage $0 source_dir dest_dir"
exit 1
}

#Main program starts here

if [ $# -ne 2 ]; then
show_usag;
else #There are two arguments
if [ -d $1 ] ;then
source_dir=$1      # source_dir 是变量, 复制表达式 等号两边不能有空格
else
echo "lnvalid source directory"
show_usag
fi
if [ -d $2 ]  ; then
dest_dir=$2
else
echo "lnvaild destination directory"
fi
fi

printf " Sorce directory is ${source_dir}\n"  #这里也是到 printf 不能“ 连在一起

printf " Destination directory is ${dest_dir}\n"
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: