目录
这是caffe里面,下载mnist数据集用到的脚本,在此记录下,也当学习一下shell,如下是脚本内容,为后续方便,添加了行号。
#!/usr/bin/env sh
# This scripts downloads the mnist data and unzips it.
DIR="$( cd "$(dirname "$0")" ; pwd -P )"
cd "$DIR"
echo "Downloading..."
for fname in train-images-idx3-ubyte train-labels-idx1-ubyte t10k-images-idx3-ubyte t10k-labels-idx1-ubyte
do
if [ ! -e $fname ]; then
wget --no-check-certificate http://yann.lecun.com/exdb/mnist/${fname}.gz
gunzip ${fname}.gz
fi
done
第一行脚本解释器
#!/usr/bin/env sh
第一行指定用到的脚本解释器(菜鸟教程),Shell 编程跟 java、php 编程一样,只要有一个能编写代码的文本编辑器和一个能解释执行的脚本解释器就可以了。Linux 的 Shell 种类众多,常见的有:
- Bourne Shell(/usr/bin/sh或/bin/sh)
- Bourne Again Shell(/bin/bash)
- C Shell(/usr/bin/csh)
- K Shell(/usr/bin/ksh)
- Shell for Root(/sbin/sh)
- ……
#! 告诉系统其后路径所指定的程序即是解释此脚本文件的 Shell 程序。
第二行注释
前面加了#号,是注释,这里这行注释描述了本脚本的功能
第三行符号用法
在读第三行之前,需要知道两个基本用法:
- 双引号中$ / " '这四个符号为特殊符号,其它符号作为普通字符处理。其中$表示变量
- $(cmd)意为执行cmd,之前也写作``
第三行是获取脚本路径的常用语句,具体参考了https://blog.csdn.net/u012336923/article/details/50312741
3. DIR="$( cd "$(dirname "$0")" ; pwd -P )"
从中间往外看:
最中间$0表示添加到shell的第0个参数,也就是shell自身的名称。如该脚本中,我输出$0,则值为get_mnist.sh,注意是带后缀的~
dirname "$0"获取该脚本所在目录,如果现在就在这个目录,会输出.
cd "$(dirname "$0")"表示进入脚本所在目录
$(cd "$(dirname "$0")" ; pwd -P )如前面所说$(cmd)意为执行cmd,这里分号分割开,说明是执行两句,分别是进入脚本目录和获取脚本目录的路径,pwd -P表示屏蔽符号连接,获取物理路径。所以$(cd "$(dirname "$0")" ; pwd -P )为进入脚本路径,并获取脚本所在物理路径。
DIR="$( cd "$(dirname "$0")" ; pwd -P )",将脚本路径赋值给DIR
为了验证如上说法,我写了个test脚本
#!/usr/bin/env sh
# This is for test!
echo "$0"
echo "$(dirname "$0")"
DIR="$( cd "$(dirname "$0")" ; pwd -P )"
echo "$DIR"
运行后的输出分别是
test.sh
.
/Users/tiantian/caffe/data/mnist
第八行if用法
if [ ! -e $fname ]; then
wget --no-check-certificate http://yann.lecun.com/exdb/mnist/${fname}.gz
gunzip ${fname}.gz
fi
-e $fname 如果fname存在则为真,前面加了叹号则反过来。也就是如果fname存在,则为假,执行then后面的语句,下载gz包,并解压