【导语】“一个冒号”通过精心收集,向本站投稿了8篇关于shell脚本编写代码格式的一个细节,以下是小编精心整理后的关于shell脚本编写代码格式的一个细节,供大家阅读参考。
篇1:关于shell脚本编写代码格式的一个细节
因为初学shell脚本编写,对其格式不是很清楚,今天写了一段脚本,练笔,发现一些问题,如下:
其中定义了一个输入变量 YES_OR_NO,
按照脚本定义的规则,当其值等于yes,则输出,
you input:yes
U r a sea man!welcome you!
当其值等于no,其值为
you input no
U r not a sea man,please exit this system
但是当脚本编写后,给脚本赋予可执行权限并编译
执行后输入结果yes
屏幕输出结果如下:
没有得到想要的结果,在第4行报错,
关于shell脚本编写代码格式的一个细节
,
考虑到有可能是条件格式的问题,因此将条件前后各个变量与括号用空格隔开如下:
编译执行后
yes
www.dnzg.cn
输入no测试
返回的还是yes的值。
所以最后,将条件格式更改为:
输入no,输出结果为:
输入yes,输出结果为
搞定,成功!
通过这个例子说明,在编写shell脚本的过程中,各个变量以及常量和描述的间距及整体的格式有时候也非常讲究,如果不注意,可能会影响程序的运行,而造成不必要的纠错开销。
以下这个例子所有代码,若有雷同,敬谅!
篇2:Shell脚本遍历一个日期范围实例
这篇文章主要介绍了Shell脚本遍历一个日期范围实例,本文直接给出实现代码,需要的朋友可以参考下
如果跟shell脚本打交道,遍历一个时间范围是很常见的事情,那么今天就跟大家分享一下:
代码如下:
#!/usr/bin/env bash
date1=“$1”
date2=“$2”
echo “date1: $date1”
echo “date2: $date2”
tempdate=`date -d “-0 day $date1” +%F`
enddate=`date -d “-0 day $date2” +%F`
tempdateSec=`date -d “-0 day $date1” +%s`
enddateSec=`date -d “-0 day $date2” +%s`
echo “####################################”
echo ‘tempdate: ‘$tempdate
echo ‘enddate: ‘$enddate
#for i in `seq 1 130`; do
for i in `seq 1 300`; do
if [[ $tempdateSec -lt $enddateSec ]]; then
break
fi
echo $tempdate
python pc_jibzhuanti_url.py $tempdate
tempdate=`date -d “-$i day $date1” +%F`
tempdateSec=`date -d “-$i day $date1” +%s`
done
这是一个倒序输出时间的脚本,需要输入两个时间参数,一个是开始时间,一个是结束时间,如输入:
代码如下:
./pc_jibzhuanti_url_run.sh 2014-06-30 2014-06-01
输出的结果是:
代码如下:
date1: 2014-06-30
date2: 2014-06-01
####################################
tempdate: 2014-06-30
enddate: 2014-06-01
2014-06-30
2014-06-29
2014-06-28
2014-06-27
2014-06-26
2014-06-25
2014-06-24
2014-06-23
2014-06-22
2014-06-21
2014-06-20
2014-06-19
2014-06-18
2014-06-17
2014-06-16
2014-06-15
2014-06-14
2014-06-13
2014-06-12
2014-06-11
2014-06-10
2014-06-09
2014-06-08
2014-06-07
2014-06-06
2014-06-05
2014-06-04
2014-06-03
2014-06-02
2014-06-01
ok,搞定!!!!
篇3:如何用 Shell 脚本编写递归程序Unix系统
UNIX Shell 脚本类似 DOS 的批处理命令,但比较起来 UNIX Shell 的功能更强大,在某些方面,Shell 甚至超过了一些高级语言, 下边的 Shell 脚本演示了如何用 Shell 脚本编写递归程序。 运行前先执行下述准备命令: ??ln tree.sh /usr/bin/tree ??ln tree
UNIX Shell 脚本类似 DOS 的批处理命令,但比较起来 UNIX Shell 的功能更强大,在某些方面,Shell 甚至超过了一些高级语言。
下边的 Shell 脚本演示了如何用 Shell 脚本编写递归程序。
运行前先执行下述准备命令:
??ln tree.sh /usr/bin/tree
??ln tree.sh /usr/bin/wtree
??ln tree.sh /usr/bin/dtree
??rm tree.sh
# tree.sh
# Depth first Directory list
dtree {
PWD=`pwd|sed 's/\\/\\$//`
for d in $*
do
echo “${PWD}/$d”
[ -d “$d” -a -x “$d” ] && {
cd “$d”
dtree *
cd ..
PWD=`pwd|sed 's/\\/\\$//` # restore PWD
}
done
}
# Depth first Directory list
wtree() {
PWD=`pwd|sed 's/\\/\\$//`
for d in $*
do
echo ${PWD}/$d
done
for d in $*
do
[ -d “$d” -a -x “$d” ] && {
cd $d
wtree *
cd ..
}
done
}
# Directory list
tree() {
PWD=`pwd|sed 's/\\/\\$//`
for d in $*
do
echo ${PWD}/$d
done
}
# main
TREE=`basename $0`
if [ “$1” ]
then DIR=“$1”
else DIR=“.”
fi
if cd $DIR
then $TREE *
else echo “$0: Directory $1 read fail.”
fi
# (End)
原文转自:www.ltesting.net
篇4:Shell脚本实现的基于SVN的代码提交量统计工具
这篇文章主要介绍了Shell脚本实现的基于SVN的代码提交量统计工具,本文直接给出实现脚本代码,需要的朋友可以参考下
最近没啥事,就用bash写了一个基于svn的代码统计小工具, 可以指定统计的目录,默认递归统计子目录。
目前还没有屏蔽指定目录的功能。哈 代码比较粗糙。不过先晒出来。
#!/bin/bash - #“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“” # FILE: lines.sh # # USAGE: ./lines.sh [dir] # AUTHOR: william # # DESCRIPTION: 基于SVN的代码提交量统计工具 # OPTIONS: --- # CREATED: 06/05/2012 12:49:20 PM CST #“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“ set -o nounset # Treat unset variables as an error # 关注的文件类型 后罪名 FILES_TYPE=”*.cpp *.h *.lua“ # 需要统计的人员,在这里写入需要统计的人,用空格隔开。哈还不智能 declare -r CODER_LIST=”coder1 coder2“ declare -i coder1 declare -i coder2 declare -r USAGE=”Usage: $0 [dir]. default dir is current dir.\\n“ # ERROR CODES; declare -r E_BAD_PATH=1 declare -r E_INVAILED_ARGU=2 declare -r E_NOT_SVN_DIR=3 #TODO 屏蔽一些dir 还没写哈 # TODO other way get path not with / end getpath() { #debug #echo dir_name: ${dir_name} #echo base_name: ${base_name} if [ $dir_name == ”/“ ] || [ $base_name == ”/“ ]; then work_path=”/“ else work_path=${dir_name}/${base_name} fi } statistic_codelines() { if [ -z ”$1“ ]; then echo ”ERROR statistic_codelines not argument“ return fi local pwd_length=${#PWD} echo ”--------------------------“ echo ”${PWD}“ for coder in $CODER_LIST; do local num=$(echo ”$1“ | grep ${coder} | wc -l) (( ${coder} += num )) if [ $num -ne 0 ]; then printf ”%10s | %-7d\\n“ ${coder} $num fi done echo ”--------------------------“ } # init check argument set work_path init_work_path() { if [ $# -eq 1 ]; then if [ $1 == ”-h“ ]; then # is help echo -e ”$USAGE“ elif [ -d $1 ]; then dir_name=$(dirname ${1}) base_name=$(basename ${1}) getpath; else echo -e ”An invailed argument“ echo -e ”Use -h get help.“ exit $E_INVAILED_ARGU fi fi } # check work_path check_work_path() { if [ -z $work_path ] || [ ! -d $work_path ]; then exit $E_BADPATH; fi } # enter work_path enter_work_path() { cd ${work_path} if [ ! $? ]; then echo ”Can not enter ${work_path} “ fi } # check work_pat is a svn dir is_svn_dir() { ( # check if current dir is asvn dir svn info &>/dev/null exit $? ) return $? } action() { local dir_name=. local base_name= local work_path=$dir_name init_work_path $1 check_work_path enter_work_path #todo can‘t enter #echo ”NOW DIR: $PWD, OLD DIR $OLDPWD“ is_svn_dir #todo to next dir local ret=$? if [ $ret -ne 0 ] then echo -e ”Current dir \\“${work_path}\\” not a svn dir.“ exit $E_NOT_SVN_DIR fi # get source files local files=$(ls ${FILES_TYPE} 2>/dev/null) if [ -n ”$files“ ]; then local namelist=$(echo -n ${files} | xargs -n 1 svn blame | awk ‘{print $2}‘) #svn blame $files #| grep $1 | wc -l statistic_codelines ”$namelist“ fi local sub_dirs=$(find -maxdepth 1 -type d -name ”[^.]*“ 2>/dev/null) if [ -n ”$sub_dirs“ ]; then for dir in $sub_dirs ; do action ”$dir“ done fi cd .. } total() { echo ”-------- TOTOAL ----------“ echo ” NAME | lines “ echo ”--------------------------“ for coder in $CODER_LIST; do if [ ${!coder} -ne 0 ]; then printf ”%10s | %-7d\\n“ ${coder} ${!coder} fi done echo ”--------------------------“ } # main echo ”-----开始统计,请耐心等待.... :) “ action $1 total exit 0
篇5:一个找后台脚本的编程代码(Python)
#/usr/bin/python import sys, os, time, httplib if sys.platform. == 'linux' or sys.platform. == 'linux2': clearing = 'clear' else: clearing = 'cls' os.system(clearing) if len(sys.argv) != 2: print \\n|------------------------------------------
篇6:编写shell脚本将VPS上的数据备份到Dropbox网盘的方法
这篇文章主要介绍了编写shell脚本将VPS上的数据备份到Dropbox网盘的方法,注意Dropbox在国内访问的网络相关问题,需要的朋友可以参考下
看到有人用dropbox备份网站数据,所以今天也试了一下,记得以前是一个python脚本,这是用的是bash 脚本,利用dropbox的api来上传下载的,很方便,脚本的地址是Dropbox-Uploader/dropbox_uploader.sh at master ・ andreafabrizi/Dropbox-Uploader ・ GitHub ,感谢作者分享这个脚本,
可以到git下载dropbox_uploader.sh,地址为:github.com/andreafabrizi/Dropbox-Uploader
或者也可以直接拷贝代码,保存为dropbox_uploader.sh,注意拷贝的时候最好是复制到文本编辑器里面,如notepad++之类的
#!/usr/bin/env bash## Dropbox Uploader## Copyright (C) 2010-2014 Andrea Fabrizi ## This program is free software; you can redistribute it and/or modify# it under the terms of the GNU General Public License as published by# the Free Software Foundation; either version 2 of the License, or# (at your option) any later version.## This program is distributed in the hope that it will be useful,# but WITHOUT ANY WARRANTY; without even the implied warranty of# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the# GNU General Public License for more details.## You should have received a copy of the GNU General Public License# along with this program; if not, write to the Free Software# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.##Default configuration fileCONFIG_FILE=~/.dropbox_uploader#Default chunk size in Mb for the upload process#It is recommended to increase this value only if you have enough free space on your /tmp partition#Lower values may increase the number of http requestsCHUNK_SIZE=4#Curl location#If not set, curl will be searched into the $PATH#CURL_BIN=”/usr/bin/curl“#Default valuesTMP_DIR=”/tmp“DEBUG=0QUIET=0SHOW_PROGRESSBAR=0SKIP_EXISTING_FILES=0ERROR_STATUS=0#Don‘t edit these...API_REQUEST_TOKEN_URL=”api.dropbox.com/1/oauth/request_token“API_USER_AUTH_URL=”www2.dropbox.com/1/oauth/authorize“API_ACCESS_TOKEN_URL=”api.dropbox.com/1/oauth/access_token“API_CHUNKED_UPLOAD_URL=”api-content.dropbox.com/1/chunked_upload“API_CHUNKED_UPLOAD_COMMIT_URL=”api-content.dropbox.com/1/commit_chunked_upload“API_UPLOAD_URL=”api-content.dropbox.com/1/files_put“API_DOWNLOAD_URL=”api-content.dropbox.com/1/files“API_DELETE_URL=”api.dropbox.com/1/fileops/delete“API_MOVE_URL=”api.dropbox.com/1/fileops/move“API_COPY_URL=”api.dropbox.com/1/fileops/copy“API_METADATA_URL=”api.dropbox.com/1/metadata“API_INFO_URL=”api.dropbox.com/1/account/info“API_MKDIR_URL=”api.dropbox.com/1/fileops/create_folder“API_SHARES_URL=”api.dropbox.com/1/shares“APP_CREATE_URL=”www2.dropbox.com/developers/apps“RESPONSE_FILE=”$TMP_DIR/du_resp_$RANDOM“CHUNK_FILE=”$TMP_DIR/du_chunk_$RANDOM“TEMP_FILE=”$TMP_DIR/du_tmp_$RANDOM“BIN_DEPS=”sed basename date grep stat dd mkdir“VERSION=”0.14“umask 077#Check the shellif [ -z ”$BASH_VERSION“ ]; then echo -e ”Error: this script. requires the BASH shell!“ exit 1fishopt -s nullglob #Bash allows filename patterns which match no files to expand to a null string, rather than themselvesshopt -s dotglob #Bash includes filenames beginning with a ”.“ in the results of filename expansion#Look for optional config file parameterwhile getopts ”:qpskdf:“ opt; do case $opt in f) CONFIG_FILE=$OPTARG ;; d) DEBUG=1 ;; q) QUIET=1 ;; p) SHOW_PROGRESSBAR=1 ;; k) CURL_ACCEPT_CERTIFICATES=”-k“ ;; s) SKIP_EXISTING_FILES=1 ;; \\?) echo ”Invalid option: -$OPTARG“ >&2 exit 1 ;; :) echo ”Option -$OPTARG requires an argument.“ >&2 exit 1 ;; esacdoneif [[ $DEBUG != 0 ]]; then echo $VERSION set -x RESPONSE_FILE=”$TMP_DIR/du_resp_debug“fiif [[ $CURL_BIN == ”“ ]]; then BIN_DEPS=”$BIN_DEPS curl“ CURL_BIN=”curl“fi#Dependencies checkwhich $BIN_DEPS >/dev/nullif [[ $? != 0 ]]; then for i in $BIN_DEPS; do which $i >/dev/null ||NOT_FOUND=”$i $NOT_FOUND“ done echo -e ”Error: Required program could not be found: $NOT_FOUND“ exit 1fi#Check if readlink is installed and supports the -m option#It‘s not necessary, so no problem if it‘s not installedwhich readlink >/dev/nullif [[ $? == 0 && $(readlink -m ”//test“ 2>/dev/null) == ”/test“ ]]; then HAVE_READLINK=1else HAVE_READLINK=0fi#Forcing to use the builtin printf, if it‘s present, because it‘s better#otherwise the external printf program will be used#Note that the external printf command can cause character encoding issues!builtin printf ”“ 2>/dev/nullif [[ $? == 0 ]]; then PRINTF=”builtin printf“ PRINTF_OPT=”-v o“else PRINTF=$(which printf) if [[ $? != 0 ]]; then echo -e ”Error: Required program could not be found: printf“ fi PRINTF_OPT=”“fi#Print the message based on $QUIET variablefunction print{ if [[ $QUIET == 0 ]]; then echo -ne ”$1“; fi}#Returns unix timestampfunction utime{ echo $(date +%s)}#Remove temporary filesfunction remove_temp_files{ if [[ $DEBUG == 0 ]]; then rm -fr ”$RESPONSE_FILE“ rm -fr ”$CHUNK_FILE“ rm -fr ”$TEMP_FILE“ fi}#Returns the file size in bytes# generic GNU Linux: linux-gnu# windows cygwin: cygwin# raspberry pi: linux-gnueabihf# macosx:darwin10.0# freebsd:FreeBSD# qnap: linux-gnueabi# iOS: darwin9function file_size{ #Some embedded linux devices if [[ $OSTYPE == ”linux-gnueabi“ || $OSTYPE == ”linux-gnu“ ]]; then stat -c ”%s“ ”$1“ return #Generic Unix elif [[ ${OSTYPE:0:5} == ”linux“ || $OSTYPE == ”cygwin“ || ${OSTYPE:0:7} == ”solaris“ ]]; then stat --format=”%s“ ”$1“ return #BSD, OSX and other OSs else stat -f ”%z“ ”$1“ return fi}#Usagefunction usage{ echo -e ”Dropbox Uploader v$VERSION“ echo -e ”Andrea Fabrizi - andrea.fabrizi@gmail.com\\n“ echo -e ”Usage: $0 COMMAND [PARAMETERS]...“ echo -e ”\\nCommands:“ echo -e ”\\t upload “ echo -e ”\\t download [LOCAL_FILE/DIR]“ echo -e ”\\t delete “ echo -e ”\\t move “ echo -e ”\\t copy “ echo -e ”\\t mkdir “ echo -e ”\\t list [REMOTE_DIR]“ echo -e ”\\t share “ echo -e ”\\t info“ echo -e ”\\t unlink“ echo -e ”\\nOptional parameters:“ echo -e ”\\t-f Load the configuration file from a specific file“ echo -e ”\\t-sSkip already existing files when download/upload. Default: Overwrite“ echo -e ”\\t-dEnable DEBUG mode“ echo -e ”\\t-qQuiet mode. Don‘t show messages“ echo -e ”\\t-pShow cURL progress meter“ echo -e ”\\t-kDoesn‘t check for SSL certificates (insecure)“ echo -en ”\\nFor more info and examples, please see the README file.\\n\\n“ remove_temp_files exit 1}#Check the curl exit codefunction check_http_response{ CODE=$? #Checking curl exit code case $CODE in #OK 0) ;; #Proxy error 5)print ”\\nError: Couldn‘t resolve proxy. The given proxy host could not be resolved.\\n“remove_temp_filesexit 1 ;; #Missing CA certificates 60|58)print ”\\nError: cURL is not able to performs peer SSL certificate verification.\\n“print ”Please, install the default ca-certificates bundle.\\n“print ”To do this in a Debian/Ubuntu based system, try:\\n“print ” sudo apt-get install ca-certificates\\n\\n“print ”If the problem persists, try to use the -k option (insecure).\\n“remove_temp_filesexit 1 ;; 6)print ”\\nError: Couldn‘t resolve host.\\n“remove_temp_filesexit 1 ;; 7)print ”\\nError: Couldn‘t connect to host.\\n“remove_temp_filesexit 1 ;; esac #Checking response file for generic errors if grep -q ”HTTP/1.1 400“ ”$RESPONSE_FILE“; then ERROR_MSG=$(sed -n -e ‘s/{”error“: ”\\([^“]*\\)”}/\\1/p‘ “$RESPONSE_FILE”) case $ERROR_MSG in *access?attempt?failed?because?this?app?is?not?configured?to?have*) echo -e “\\nError: The Permission type/Access level configured doesn‘t match the DropBox App settings!\\nPlease run \\”$0 unlink\\“ and try again.” exit 1;; esac fi}#Urlencodefunction urlencode{ local string=“${1}” local strlen=${#string} local encoded=“” for (( pos=0 ; pos/dev/null check_http_response #Even if the file/dir has been deleted from DropBox we receive a 200 OK response #So we must check if the file exists or if it has been deleted if grep -q “\\”is_deleted\\“:” “$RESPONSE_FILE”; then local IS_DELETED=$(sed -n ‘s/.*“is_deleted”:.\\([^,]*\\).*/\\1/p‘ “$RESPONSE_FILE”) else local IS_DELETED=“false” fi #Exits... grep -q “^HTTP/1.1 200 OK” “$RESPONSE_FILE” if [[ $? == 0 && $IS_DELETED != “true” ]]; then local IS_DIR=$(sed -n ‘s/^\\(.*\\)\\“contents”:.\\[.*/\\1/p‘ “$RESPONSE_FILE”) #It‘s a directory if [[ $IS_DIR != “” ]]; thenecho “DIR” #It‘s a file elseecho “FILE” fi #Doesn‘t exists else echo “ERR” fi}#Generic upload wrapper around db_upload_file and db_upload_dir functions#$1 = Local source file/dir#$2 = Remote destination file/dirfunction db_upload{ local SRC=$(normalize_path “$1”) local DST=$(normalize_path “$2”) #Checking if the file/dir exists if [[ ! -e $SRC && ! -d $SRC ]]; then print “ >No such file or directory: $SRC\\n” ERROR_STATUS=1 return fi #Checking if the file/dir has read permissions if [[ ! -r $SRC ]]; then print “ >Error reading file $SRC: permission denied\\n” ERROR_STATUS=1 return fi #Checking if DST it‘s a folder or if it doesn‘ exists (in this case will be the destination name) TYPE=$(db_stat “$DST”) if [[ $TYPE == “DIR” ]]; then local filename=$(basename “$SRC”) DST=“$DST/$filename” fi #It‘s a directory if [[ -d $SRC ]]; then db_upload_dir “$SRC” “$DST” #It‘s a file elif [[ -e $SRC ]]; then db_upload_file “$SRC” “$DST” #Unsupported object... else print “ >Skipping not regular file \\”$SRC\\“\\n” fi}#Generic upload wrapper around db_chunked_upload_file and db_simple_upload_file#The final upload function will be choosen based on the file size#$1 = Local source file#$2 = Remote destination filefunction db_upload_file{ local FILE_SRC=$(normalize_path “$1”) local FILE_DST=$(normalize_path “$2”) shopt -s nocasematch #Checking not allowed file names basefile_dst=$(basename “$FILE_DST”) if [[ $basefile_dst == “thumbs.db” || \\ $basefile_dst == “desktop.ini” || \\ $basefile_dst == “.ds_store” || \\ $basefile_dst == “icon\\r” || \\ $basefile_dst == “.dropbox” || \\ $basefile_dst == “.dropbox.attr” \\ ]]; then print “ >Skipping not allowed file name \\”$FILE_DST\\“\\n” return fi shopt -u nocasematch #Checking file size FILE_SIZE=$(file_size “$FILE_SRC”) #Checking if the file already exists TYPE=$(db_stat “$FILE_DST”) if [[ $TYPE != “ERR” && $SKIP_EXISTING_FILES == 1 ]]; then print “ >Skipping already existing file \\”$FILE_DST\\“\\n” return fi if [[ $FILE_SIZE -gt 157286000 ]]; then #If the file is greater than 150Mb, the chunked_upload API will be used db_chunked_upload_file “$FILE_SRC” “$FILE_DST” else db_simple_upload_file “$FILE_SRC” “$FILE_DST” fi}#Simple file upload#$1 = Local source file#$2 = Remote destination filefunction db_simple_upload_file{ local FILE_SRC=$(normalize_path “$1”) local FILE_DST=$(normalize_path “$2”) if [[ $SHOW_PROGRESSBAR == 1 && $QUIET == 0 ]]; then CURL_PARAMETERS=“--progress-bar” LINE_CR=“\\n” else CURL_PARAMETERS=“-s” LINE_CR=“” fi print “ >Uploading \\”$FILE_SRC\\“ to \\”$FILE_DST\\“... $LINE_CR” $CURL_BIN $CURL_ACCEPT_CERTIFICATES $CURL_PARAMETERS -i --globoff -o “$RESPONSE_FILE” --upload-file “$FILE_SRC” “$API_UPLOAD_URL/$ACCESS_LEVEL/$(urlencode ”$FILE_DST“)?oauth_consumer_key=$APPKEY&oauth_token=$OAUTH_ACCESS_TOKEN&oauth_signature_method=PLAINTEXT&oauth_signature=$APPSECRET%26$OAUTH_ACCESS_TOKEN_SECRET&oauth_timestamp=$(utime)&oauth_nonce=$RANDOM” check_http_response #Check if grep -q “^HTTP/1.1 200 OK” “$RESPONSE_FILE”; then print “DONE\\n” else print “FAILED\\n” print “An error occurred requesting /upload\\n” ERROR_STATUS=1 fi}#Chunked file upload#$1 = Local source file#$2 = Remote destination filefunction db_chunked_upload_file{ local FILE_SRC=$(normalize_path “$1”) local FILE_DST=$(normalize_path “$2”) print “ >Uploading \\”$FILE_SRC\\“ to \\”$FILE_DST\\“” local FILE_SIZE=$(file_size “$FILE_SRC”) local FFSET=0 local UPLOAD_ID=“” local UPLOAD_ERROR=0 local CHUNK_PARAMS=“” #Uploading chunks... while ([[ $OFFSET != $FILE_SIZE ]]); do let OFFSET_MB=$OFFSET/1024/1024 #Create the chunk dd if=“$FILE_SRC” f=“$CHUNK_FILE” bs=1048576 skip=$OFFSET_MB count=$CHUNK_SIZE 2>/dev/null #Only for the first request these parameters are not included if [[ $OFFSET != 0 ]]; thenCHUNK_PARAMS=“upload_id=$UPLOAD_ID&offset=$OFFSET” fi #Uploading the chunk... $CURL_BIN $CURL_ACCEPT_CERTIFICATES -s --show-error --globoff -i -o “$RESPONSE_FILE” --upload-file “$CHUNK_FILE” “$API_CHUNKED_UPLOAD_URL?$CHUNK_PARAMS&oauth_consumer_key=$APPKEY&oauth_token=$OAUTH_ACCESS_TOKEN&oauth_signature_method=PLAINTEXT&oauth_signature=$APPSECRET%26$OAUTH_ACCESS_TOKEN_SECRET&oauth_timestamp=$(utime)&oauth_nonce=$RANDOM” 2>/dev/null check_http_response #Check if grep -q “^HTTP/1.1 200 OK” “$RESPONSE_FILE”; thenprint “.”UPLOAD_ERROR=0UPLOAD_ID=$(sed -n ‘s/.*“upload_id”: *“*\\([^”]*\\)“*.*/\\1/p‘ ”$RESPONSE_FILE“)FFSET=$(sed -n ‘s/.*”offset“: *\\([^}]*\\).*/\\1/p‘ ”$RESPONSE_FILE“) elseprint ”*“let UPLOAD_ERROR=$UPLOAD_ERROR+1#On error, the upload is retried for max 3 timesif [[ $UPLOAD_ERROR -gt 2 ]]; then print ” FAILED\\n“ print ”An error occurred requesting /chunked_upload\\n“ ERROR_STATUS=1 returnfi fi done UPLOAD_ERROR=0 #Commit the upload while (true); do $CURL_BIN $CURL_ACCEPT_CERTIFICATES -s --show-error --globoff -i -o ”$RESPONSE_FILE“ --data ”upload_id=$UPLOAD_ID&oauth_consumer_key=$APPKEY&oauth_token=$OAUTH_ACCESS_TOKEN&oauth_signature_method=PLAINTEXT&oauth_signature=$APPSECRET%26$OAUTH_ACCESS_TOKEN_SECRET&oauth_timestamp=$(utime)&oauth_nonce=$RANDOM“ ”$API_CHUNKED_UPLOAD_COMMIT_URL/$ACCESS_LEVEL/$(urlencode “$FILE_DST”)“ 2>/dev/null check_http_response #Check if grep -q ”^HTTP/1.1 200 OK“ ”$RESPONSE_FILE“; thenprint ”.“UPLOAD_ERROR=0break elseprint ”*“let UPLOAD_ERROR=$UPLOAD_ERROR+1#On error, the commit is retried for max 3 timesif [[ $UPLOAD_ERROR -gt 2 ]]; then print ” FAILED\\n“ print ”An error occurred requesting /commit_chunked_upload\\n“ ERROR_STATUS=1 returnfi fi done print ” DONE\\n“}#Directory upload#$1 = Local source dir#$2 = Remote destination dirfunction db_upload_dir{ local DIR_SRC=$(normalize_path ”$1“) local DIR_DST=$(normalize_path ”$2“) #Creatig remote directory db_mkdir ”$DIR_DST“ for file in ”$DIR_SRC/“*; do db_upload ”$file“ ”$DIR_DST“ done}#Returns the free space on DropBox in bytesfunction db_free_quota{ $CURL_BIN $CURL_ACCEPT_CERTIFICATES -s --show-error --globoff -i -o ”$RESPONSE_FILE“ --data ”oauth_consumer_key=$APPKEY&oauth_token=$OAUTH_ACCESS_TOKEN&oauth_signature_method=PLAINTEXT&oauth_signature=$APPSECRET%26$OAUTH_ACCESS_TOKEN_SECRET&oauth_timestamp=$(utime)&oauth_nonce=$RANDOM“ ”$API_INFO_URL“ 2>/dev/null check_http_response #Check if grep -q ”^HTTP/1.1 200 OK“ ”$RESPONSE_FILE“; then quota=$(sed -n ‘s/.*”quota“: \\([0-9]*\\).*/\\1/p‘ ”$RESPONSE_FILE“) used=$(sed -n ‘s/.*”normal“: \\([0-9]*\\).*/\\1/p‘ ”$RESPONSE_FILE“) let free_quota=$quota-$used echo $free_quota else echo 0 fi}#Generic download wrapper#$1 = Remote source file/dir#$2 = Local destination file/dirfunction db_download{ local SRC=$(normalize_path ”$1“) local DST=$(normalize_path ”$2“) TYPE=$(db_stat ”$SRC“) #It‘s a directory if [[ $TYPE == ”DIR“ ]]; then #If the DST folder is not specified, I assume that is the current directory if [[ $DST == ”“ ]]; thenDST=”.“ fi #Checking if the destination directory exists if [[ ! -d $DST ]]; thenlocal basedir=”“ elselocal basedir=$(basename ”$SRC“) fi local DEST_DIR=$(normalize_path ”$DST/$basedir“) print ” >Downloading \\“$SRC\\” to \\“$DEST_DIR\\”... \\n“ print ” >Creating local directory \\“$DEST_DIR\\”... “ mkdir -p ”$DEST_DIR“ #Check if [[ $? == 0 ]]; thenprint ”DONE\\n“ elseprint ”FAILED\\n“ERROR_STATUS=1return fi #Extracting directory content [...] #and replacing ”}, {“ with ”}\\n{“ #I don‘t like this piece of code... but seems to be the only way to do this with SED, writing a portable code... local DIR_CONTENT=$(sed -n ‘s/.*: \\[{\\(.*\\)/\\1/p‘ ”$RESPONSE_FILE“ | sed ‘s/}, *{/}\\{/g‘) #Extracting files and subfolders TMP_DIR_CONTENT_FILE=”${RESPONSE_FILE}_$RANDOM“ echo ”$DIR_CONTENT“ | sed -n ‘s/.*”path“: *”\\([^“]*\\)”,.*“is_dir”: *\\([^“]*\\),.*/\\1:\\2/p‘ >$TMP_DIR_CONTENT_FILE #For each entry... while read -r line; dolocal FILE=${line%:*}local TYPE=${line#*:}#Removing unneeded /FILE=${FILE##*/}if [[ $TYPE == ”false“ ]]; then db_download_file ”$SRC/$FILE“ ”$DEST_DIR/$FILE“else db_download ”$SRC/$FILE“ ”$DEST_DIR“fi done < $TMP_DIR_CONTENT_FILE rm -fr $TMP_DIR_CONTENT_FILE #It‘s a file elif [[ $TYPE == ”FILE“ ]]; then #Checking DST if [[ $DST == ”“ ]]; thenDST=$(basename ”$SRC“) fi #If the destination is a directory, the file will be download into if [[ -d $DST ]]; thenDST=”$DST/$SRC“ fi db_download_file ”$SRC“ ”$DST“ #Doesn‘t exists else print ” >No such file or directory: $SRC\\n“ ERROR_STATUS=1 return fi}#Simple file download#$1 = Remote source file#$2 = Local destination filefunction db_download_file{ local FILE_SRC=$(normalize_path ”$1“) local FILE_DST=$(normalize_path ”$2“) if [[ $SHOW_PROGRESSBAR == 1 && $QUIET == 0 ]]; then CURL_PARAMETERS=”--progress-bar“ LINE_CR=”\\n“ else CURL_PARAMETERS=”-s“ LINE_CR=”“ fi #Checking if the file already exists if [[ -e $FILE_DST && $SKIP_EXISTING_FILES == 1 ]]; then print ” >Skipping already existing file \\“$FILE_DST\\”\\n“ return fi #Creating the empty file, that for two reasons: #1) In this way I can check if the destination file is writable or not #2) Curl doesn‘t automatically creates files with 0 bytes size dd if=/dev/zero f=”$FILE_DST“ count=0 2>/dev/null if [[ $? != 0 ]]; then print ” >Error writing file $FILE_DST: permission denied\\n“ ERROR_STATUS=1 return fi print ” >Downloading \\“$FILE_SRC\\” to \\“$FILE_DST\\”... $LINE_CR“ $CURL_BIN $CURL_ACCEPT_CERTIFICATES $CURL_PARAMETERS --globoff -D ”$RESPONSE_FILE“ -o ”$FILE_DST“ ”$API_DOWNLOAD_URL/$ACCESS_LEVEL/$(urlencode “$FILE_SRC”)?oauth_consumer_key=$APPKEY&oauth_token=$OAUTH_ACCESS_TOKEN&oauth_signature_method=PLAINTEXT&oauth_signature=$APPSECRET%26$OAUTH_ACCESS_TOKEN_SECRET&oauth_timestamp=$(utime)&oauth_nonce=$RANDOM“ check_http_response #Check if grep -q ”^HTTP/1.1 200 OK“ ”$RESPONSE_FILE“; then print ”DONE\\n“ else print ”FAILED\\n“ rm -fr ”$FILE_DST“ ERROR_STATUS=1 return fi}#Prints account infofunction db_account_info{ print ”Dropbox Uploader v$VERSION\\n\\n“ print ” >Getting info... “ $CURL_BIN $CURL_ACCEPT_CERTIFICATES -s --show-error --globoff -i -o ”$RESPONSE_FILE“ --data ”oauth_consumer_key=$APPKEY&oauth_token=$OAUTH_ACCESS_TOKEN&oauth_signature_method=PLAINTEXT&oauth_signature=$APPSECRET%26$OAUTH_ACCESS_TOKEN_SECRET&oauth_timestamp=$(utime)&oauth_nonce=$RANDOM“ ”$API_INFO_URL“ 2>/dev/null check_http_response #Check if grep -q ”^HTTP/1.1 200 OK“ ”$RESPONSE_FILE“; then name=$(sed -n ‘s/.*”display_name“: ”\\([^“]*\\).*/\\1/p‘ ”$RESPONSE_FILE“) echo -e ”\\n\\nName:\\t$name“ uid=$(sed -n ‘s/.*”uid“: \\([0-9]*\\).*/\\1/p‘ ”$RESPONSE_FILE“) echo -e ”UID:\\t$uid“ email=$(sed -n ‘s/.*”email“: ”\\([^“]*\\).*/\\1/p‘ ”$RESPONSE_FILE“) echo -e ”Email:\\t$email“ quota=$(sed -n ‘s/.*”quota“: \\([0-9]*\\).*/\\1/p‘ ”$RESPONSE_FILE“) let quota_mb=$quota/1024/1024 echo -e ”Quota:\\t$quota_mb Mb“ used=$(sed -n ‘s/.*”normal“: \\([0-9]*\\).*/\\1/p‘ ”$RESPONSE_FILE“) let used_mb=$used/1024/1024 echo -e ”Used:\\t$used_mb Mb“ let free_mb=($quota-$used)/1024/1024 echo -e ”Free:\\t$free_mb Mb“ echo ”“ else print ”FAILED\\n“ ERROR_STATUS=1 fi}#Account unlinkfunction db_unlink{ echo -ne ”Are you sure you want unlink this script. from your Dropbox account? [y/n]“ read answer if [[ $answer == ”y“ ]]; then rm -fr ”$CONFIG_FILE“ echo -ne ”DONE\\n“ fi}#Delete a remote file#$1 = Remote file to deletefunction db_delete{ local FILE_DST=$(normalize_path ”$1“) print ” >Deleting \\“$FILE_DST\\”... “ $CURL_BIN $CURL_ACCEPT_CERTIFICATES -s --show-error --globoff -i -o ”$RESPONSE_FILE“ --data ”oauth_consumer_key=$APPKEY&oauth_token=$OAUTH_ACCESS_TOKEN&oauth_signature_method=PLAINTEXT&oauth_signature=$APPSECRET%26$OAUTH_ACCESS_TOKEN_SECRET&oauth_timestamp=$(utime)&oauth_nonce=$RANDOM&root=$ACCESS_LEVEL&path=$(urlencode “$FILE_DST”)“ ”$API_DELETE_URL“ 2>/dev/null check_http_response #Check if grep -q ”^HTTP/1.1 200 OK“ ”$RESPONSE_FILE“; then print ”DONE\\n“ else print ”FAILED\\n“ ERROR_STATUS=1 fi}#Move/Rename a remote file#$1 = Remote file to rename or move#$2 = New file name or locationfunction db_move{ local FILE_SRC=$(normalize_path ”$1“) local FILE_DST=$(normalize_path ”$2“) TYPE=$(db_stat ”$FILE_DST“) #If the destination it‘s a directory, the source will be moved into it if [[ $TYPE == ”DIR“ ]]; then local filename=$(basename ”$FILE_SRC“) FILE_DST=$(normalize_path ”$FILE_DST/$filename“) fi print ” >Moving \\“$FILE_SRC\\” to \\“$FILE_DST\\” ... “ $CURL_BIN $CURL_ACCEPT_CERTIFICATES -s --show-error --globoff -i -o ”$RESPONSE_FILE“ --data ”oauth_consumer_key=$APPKEY&oauth_token=$OAUTH_ACCESS_TOKEN&oauth_signature_method=PLAINTEXT&oauth_signature=$APPSECRET%26$OAUTH_ACCESS_TOKEN_SECRET&oauth_timestamp=$(utime)&oauth_nonce=$RANDOM&root=$ACCESS_LEVEL&from_path=$(urlencode “$FILE_SRC”)&to_path=$(urlencode “$FILE_DST”)“ ”$API_MOVE_URL“ 2>/dev/null check_http_response #Check if grep -q ”^HTTP/1.1 200 OK“ ”$RESPONSE_FILE“; then print ”DONE\\n“ else print ”FAILED\\n“ ERROR_STATUS=1 fi}#Copy a remote file to a remote location#$1 = Remote file to rename or move#$2 = New file name or locationfunction db_copy{ local FILE_SRC=$(normalize_path ”$1“) local FILE_DST=$(normalize_path ”$2“) TYPE=$(db_stat ”$FILE_DST“) #If the destination it‘s a directory, the source will be copied into it if [[ $TYPE == ”DIR“ ]]; then local filename=$(basename ”$FILE_SRC“) FILE_DST=$(normalize_path ”$FILE_DST/$filename“) fi print ” >Copying \\“$FILE_SRC\\” to \\“$FILE_DST\\” ... “ $CURL_BIN $CURL_ACCEPT_CERTIFICATES -s --show-error --globoff -i -o ”$RESPONSE_FILE“ --data ”oauth_consumer_key=$APPKEY&oauth_token=$OAUTH_ACCESS_TOKEN&oauth_signature_method=PLAINTEXT&oauth_signature=$APPSECRET%26$OAUTH_ACCESS_TOKEN_SECRET&oauth_timestamp=$(utime)&oauth_nonce=$RANDOM&root=$ACCESS_LEVEL&from_path=$(urlencode “$FILE_SRC”)&to_path=$(urlencode “$FILE_DST”)“ ”$API_COPY_URL“ 2>/dev/null check_http_response #Check if grep -q ”^HTTP/1.1 200 OK“ ”$RESPONSE_FILE“; then print ”DONE\\n“ else print ”FAILED\\n“ ERROR_STATUS=1 fi}#Create a new directory#$1 = Remote directory to createfunction db_mkdir{ local DIR_DST=$(normalize_path ”$1“) print ” >Creating Directory \\“$DIR_DST\\”... “ $CURL_BIN $CURL_ACCEPT_CERTIFICATES -s --show-error --globoff -i -o ”$RESPONSE_FILE“ --data ”oauth_consumer_key=$APPKEY&oauth_token=$OAUTH_ACCESS_TOKEN&oauth_signature_method=PLAINTEXT&oauth_signature=$APPSECRET%26$OAUTH_ACCESS_TOKEN_SECRET&oauth_timestamp=$(utime)&oauth_nonce=$RANDOM&root=$ACCESS_LEVEL&path=$(urlencode “$DIR_DST”)“ ”$API_MKDIR_URL“ 2>/dev/null check_http_response #Check if grep -q ”^HTTP/1.1 200 OK“ ”$RESPONSE_FILE“; then print ”DONE\\n“ elif grep -q ”^HTTP/1.1 403 Forbidden“ ”$RESPONSE_FILE“; then print ”ALREADY EXISTS\\n“ else print ”FAILED\\n“ ERROR_STATUS=1 fi}#List remote directory#$1 = Remote directoryfunction db_list{ local DIR_DST=$(normalize_path ”$1“) print ” >Listing \\“$DIR_DST\\”... “ $CURL_BIN $CURL_ACCEPT_CERTIFICATES -s --show-error --globoff -i -o ”$RESPONSE_FILE“ ”$API_METADATA_URL/$ACCESS_LEVEL/$(urlencode “$DIR_DST”)?oauth_consumer_key=$APPKEY&oauth_token=$OAUTH_ACCESS_TOKEN&oauth_signature_method=PLAINTEXT&oauth_signature=$APPSECRET%26$OAUTH_ACCESS_TOKEN_SECRET&oauth_timestamp=$(utime)&oauth_nonce=$RANDOM“ 2>/dev/null check_http_response #Check if grep -q ”^HTTP/1.1 200 OK“ ”$RESPONSE_FILE“; then local IS_DIR=$(sed -n ‘s/^\\(.*\\)\\”contents“:.\\[.*/\\1/p‘ ”$RESPONSE_FILE“) #It‘s a directory if [[ $IS_DIR != ”“ ]]; thenprint ”DONE\\n“#Extracting directory content [...]#and replacing ”}, {“ with ”}\\n{“#I don‘t like this piece of code... but seems to be the only way to do this with SED, writing a portable code...local DIR_CONTENT=$(sed -n ‘s/.*: \\[{\\(.*\\)/\\1/p‘ ”$RESPONSE_FILE“ | sed ‘s/}, *{/}\\{/g‘)#Converting escaped quotes to unicode formatecho ”$DIR_CONTENT“ | sed ‘s/\\\\”/\\\\u0022/‘ >“$TEMP_FILE”#Extracting files and subfoldersrm -fr “$RESPONSE_FILE”while read -r line; do local FILE=$(echo “$line” | sed -n ‘s/.*“path”: *“\\([^”]*\\)“.*/\\1/p‘) local IS_DIR=$(echo ”$line“ | sed -n ‘s/.*”is_dir“: *\\([^,]*\\).*/\\1/p‘) local SIZE=$(echo ”$line“ | sed -n ‘s/.*”bytes“: *\\([0-9]*\\).*/\\1/p‘) echo -e ”$FILE:$IS_DIR;$SIZE“ >>”$RESPONSE_FILE“done < ”$TEMP_FILE“#Looking for the biggest file size#to calculate the padding to uselocal padding=0while read -r line; do local FILE=${line%:*} local META=${line##*:} local SIZE=${META#*;} if [[ ${#SIZE} -gt $padding ]]; then padding=${#SIZE} fidone < ”$RESPONSE_FILE“#For each entry, printing directories...while read -r line; do local FILE=${line%:*} local META=${line##*:} local TYPE=${META%;*} local SIZE=${META#*;} #Removing unneeded / FILE=${FILE##*/} if [[ $TYPE != ”false“ ]]; then FILE=$(echo -e ”$FILE“) $PRINTF ” [D] %-${padding}s %s\\n“ ”$SIZE“ ”$FILE“ fidone < ”$RESPONSE_FILE“#For each entry, printing files...while read -r line; do local FILE=${line%:*} local META=${line##*:} local TYPE=${META%;*} local SIZE=${META#*;} #Removing unneeded / FILE=${FILE##*/} if [[ $TYPE == ”false“ ]]; then FILE=$(echo -e ”$FILE“) $PRINTF ” [F] %-${padding}s %s\\n“ ”$SIZE“ ”$FILE“ fidone < ”$RESPONSE_FILE“ #It‘s a file elseprint ”FAILED: $DIR_DST is not a directory!\\n“ERROR_STATUS=1 fi else print ”FAILED\\n“ ERROR_STATUS=1 fi}#Share remote file#$1 = Remote filefunction db_share{ local FILE_DST=$(normalize_path ”$1“) $CURL_BIN $CURL_ACCEPT_CERTIFICATES -s --show-error --globoff -i -o ”$RESPONSE_FILE“ ”$API_SHARES_URL/$ACCESS_LEVEL/$(urlencode “$FILE_DST”)?oauth_consumer_key=$APPKEY&oauth_token=$OAUTH_ACCESS_TOKEN&oauth_signature_method=PLAINTEXT&oauth_signature=$APPSECRET%26$OAUTH_ACCESS_TOKEN_SECRET&oauth_timestamp=$(utime)&oauth_nonce=$RANDOM&short_url=false“ 2>/dev/null check_http_response #Check if grep -q ”^HTTP/1.1 200 OK“ ”$RESPONSE_FILE“; then print ” >Share link: “ echo $(sed -n ‘s/.*”url“: ”\\([^“]*\\).*/\\1/p‘ ”$RESPONSE_FILE“) else print ”FAILED\\n“ ERROR_STATUS=1 fi}#################### SETUP #####################CHECKING FOR AUTH FILEif [[ -e $CONFIG_FILE ]]; then #Loading data... and change old format config if necesary. source ”$CONFIG_FILE“ 2>/dev/null || { sed -i‘‘ ‘s/:/=/‘ ”$CONFIG_FILE“ && source ”$CONFIG_FILE“ 2>/dev/null } #Checking the loaded data if [[ $APPKEY == ”“ || $APPSECRET == ”“ || $OAUTH_ACCESS_TOKEN_SECRET == ”“ || $OAUTH_ACCESS_TOKEN == ”“ ]]; then echo -ne ”Error loading data from $CONFIG_FILE...\\n“ echo -ne ”It is recommended to run $0 unlink\\n“ remove_temp_files exit 1 fi #Back compatibility with previous Dropbox Uploader versions if [[ $ACCESS_LEVEL == ”“ ]]; then ACCESS_LEVEL=”dropbox“ fi#NEW SETUP...else echo -ne ”\\n This is the first time you run this script.\\n\\n“ echo -ne ” 1) Open the following URL in your Browser, and log in using your account: $APP_CREATE_URL\\n“ echo -ne ” 2) Click on \\“Create App\\”, then select \\“Dropbox API app\\”\\n“ echo -ne ” 3) Select \\“Files and datastores\\”\\n“ echo -ne ” 4) Now go on with the configuration, choosing the app permissions and access restrictions to your DropBox folder\\n“ echo -ne ” 5) Enter the \\“App Name\\” that you prefer (e.g. MyUploader$RANDOM$RANDOM$RANDOM)\\n\\n“ echo -ne ” Now, click on the \\“Create App\\” button.\\n\\n“ echo -ne ” When your new App is successfully created, please type the\\n“ echo -ne ” App Key, App Secret and the Permission type shown in the confirmation page:\\n\\n“ #Getting the app key and secret from the user while (true); do echo -n ” # App key: “ read APPKEY echo -n ” # App secret: “ read APPSECRET echo -n ” # Permission type, App folder or Full Dropbox [a/f]: “ read ACCESS_LEVEL if [[ $ACCESS_LEVEL == ”a“ ]]; thenACCESS_LEVEL=”sandbox“ACCESS_MSG=”App Folder“ elseACCESS_LEVEL=”dropbox“ACCESS_MSG=”Full Dropbox“ fi echo -ne ”\\n >App key is $APPKEY, App secret is $APPSECRET and Access level is $ACCESS_MSG. Looks ok? [y/n]: “ read answer if [[ $answer == ”y“ ]]; thenbreak; fi done #TOKEN REQUESTS echo -ne ”\\n >Token request... “ $CURL_BIN $CURL_ACCEPT_CERTIFICATES -s --show-error --globoff -i -o ”$RESPONSE_FILE“ --data ”oauth_consumer_key=$APPKEY&oauth_signature_method=PLAINTEXT&oauth_signature=$APPSECRET%26&oauth_timestamp=$(utime)&oauth_nonce=$RANDOM“ ”$API_REQUEST_TOKEN_URL“ 2>/dev/null check_http_response OAUTH_TOKEN_SECRET=$(sed -n ‘s/oauth_token_secret=\\([a-z A-Z 0-9]*\\).*/\\1/p‘ ”$RESPONSE_FILE“) OAUTH_TOKEN=$(sed -n ‘s/.*oauth_token=\\([a-z A-Z 0-9]*\\)/\\1/p‘ ”$RESPONSE_FILE“) if [[ $OAUTH_TOKEN != ”“ && $OAUTH_TOKEN_SECRET != ”“ ]]; then echo -ne ”OK\\n“ else echo -ne ” FAILED\\n\\n Please, check your App key and secret...\\n\\n“ remove_temp_files exit 1 fi while (true); do #USER AUTH echo -ne ”\\n Please open the following URL in your browser, and allow Dropbox Uploader\\n“ echo -ne ” to access your DropBox folder:\\n\\n -->${API_USER_AUTH_URL}?oauth_token=$OAUTH_TOKEN\\n“ echo -ne ”\\nPress enter when done...\\n“ read #API_ACCESS_TOKEN_URL echo -ne ” >Access Token request... “ $CURL_BIN $CURL_ACCEPT_CERTIFICATES -s --show-error --globoff -i -o ”$RESPONSE_FILE“ --data ”oauth_consumer_key=$APPKEY&oauth_token=$OAUTH_TOKEN&oauth_signature_method=PLAINTEXT&oauth_signature=$APPSECRET%26$OAUTH_TOKEN_SECRET&oauth_timestamp=$(utime)&oauth_nonce=$RANDOM“ ”$API_ACCESS_TOKEN_URL“ 2>/dev/null check_http_response OAUTH_ACCESS_TOKEN_SECRET=$(sed -n ‘s/oauth_token_secret=\\([a-z A-Z 0-9]*\\)&.*/\\1/p‘ ”$RESPONSE_FILE“) OAUTH_ACCESS_TOKEN=$(sed -n ‘s/.*oauth_token=\\([a-z A-Z 0-9]*\\)&.*/\\1/p‘ ”$RESPONSE_FILE“) OAUTH_ACCESS_UID=$(sed -n ‘s/.*uid=\\([0-9]*\\)/\\1/p‘ ”$RESPONSE_FILE“) if [[ $OAUTH_ACCESS_TOKEN != ”“ && $OAUTH_ACCESS_TOKEN_SECRET != ”“ && $OAUTH_ACCESS_UID != ”“ ]]; thenecho -ne ”OK\\n“#Saving data in new format, compatible with source command.echo ”APPKEY=$APPKEY“ >”$CONFIG_FILE“echo ”APPSECRET=$APPSECRET“ >>”$CONFIG_FILE“echo ”ACCESS_LEVEL=$ACCESS_LEVEL“ >>”$CONFIG_FILE“echo ”OAUTH_ACCESS_TOKEN=$OAUTH_ACCESS_TOKEN“ >>”$CONFIG_FILE“echo ”OAUTH_ACCESS_TOKEN_SECRET=$OAUTH_ACCESS_TOKEN_SECRET“ >>”$CONFIG_FILE“echo -ne ”\\n Setup completed!\\n“break elseprint ” FAILED\\n“ERROR_STATUS=1 fi done; remove_temp_files exit $ERROR_STATUSfi#################### START ####################COMMAND=${@:$OPTIND:1}ARG1=${@:$OPTIND+1:1}ARG2=${@:$OPTIND+2:1}let argnum=$#-$OPTIND#CHECKING PARAMS VALUEScase $COMMAND in upload) if [[ $argnum -lt 2 ]]; thenusage fi FILE_DST=${@:$#:1} for (( i=$OPTIND+1; i<$#; i++ )); doFILE_SRC=${@:$i:1}db_upload ”$FILE_SRC“ ”/$FILE_DST“ done ;; download) if [[ $argnum -lt 1 ]]; thenusage fi FILE_SRC=$ARG1 FILE_DST=$ARG2 db_download ”/$FILE_SRC“ ”$FILE_DST“ ;; share) if [[ $argnum -lt 1 ]]; thenusage fi FILE_DST=$ARG1 db_share ”/$FILE_DST“ ;; info) db_account_info ;; delete|remove) if [[ $argnum -lt 1 ]]; thenusage fi FILE_DST=$ARG1 db_delete ”/$FILE_DST“ ;; move|rename) if [[ $argnum -lt 2 ]]; thenusage fi FILE_SRC=$ARG1 FILE_DST=$ARG2 db_move ”/$FILE_SRC“ ”/$FILE_DST“ ;; copy) if [[ $argnum -lt 2 ]]; thenusage fi FILE_SRC=$ARG1 FILE_DST=$ARG2 db_copy ”/$FILE_SRC“ ”/$FILE_DST“ ;; mkdir) if [[ $argnum -lt 1 ]]; thenusage fi DIR_DST=$ARG1 db_mkdir ”/$DIR_DST“ ;; list) DIR_DST=$ARG1 #Checking DIR_DST if [[ $DIR_DST == ”“ ]]; thenDIR_DST=”/“ fi db_list ”/$DIR_DST“ ;; unlink) db_unlink ;; *) if [[ $COMMAND != ”“ ]]; thenprint ”Error: Unknown command: $COMMAND\\n\\n“ERROR_STATUS=1 fi usage ;;esacremove_temp_filesexit $ERROR_STATUS第一次使用,这个脚本会给你指导,告诉你去哪里获得dropbox的 API key ,
因为只有配置好了 API key ,这样才能授权给脚本应用进入你的dropbox目录
篇7:请教:怎样编写杀死HPUnix的一个进程的脚本!!Windows系统
coolscplayer 回复于:2003-08-22 12:09:20 wh_pd=1 while[$wh_pd-eq1] do killid=`ps-eaf|grepurp|grep-vgrep|awk'{print$2}` if[-z$killid] then cotinue else kill-9$killid wh_pd=0 fi done williamCU 回复于:2003-09-01 20:21:06 楼上这么复杂? kill$
coolscplayer 回复于:2003-08-22 12:09:20wh_pd=1
while [ $wh_pd -eq 1 ]
do
killid=`ps -eaf|grep urp|grep -v grep|awk '{print $2}`
if [ -z $killid ]
then
cotinue
else
kill -9 $killid
wh_pd=0
fi
done
williamCU 回复于:2003-09-01 20:21:06楼上这么复杂?
kill $(ps -ef|grep myproc|grep -v grep|cut -c 10-14)
以上kill一个进程名为myproc的进程,
albert 回复于:2003-09-01 20:41:09[quote:84ac1a5999=”williamCU“]楼上这么复杂?
kill $(ps -ef|grep myproc|grep -v grep|cut -c 10-14)
以上kill一个进程名为myproc的进程。[/quote:84ac1a5999]
如果还有一个进程叫myproc1呢? 呵呵。
williamCU 回复于:2003-09-01 21:39:57楼上的楼上也不能啊!呵呵呵。。
williamCU 回复于:2003-09-02 10:29:15kill $(ps -ef|grep myproc|grep -v grep|cut -c 10-14)
可能我没说清楚,
这条语句可以kill掉所有包含myproc字样的进程,
可以达到coolscplayer 的那个script的功能。
我说的不能,是指不能杀死任何进程,但可以带参数来实现:
将以上写成一个名为mykill的脚本:
vi mykill
kill $(ps -ef|grep $1|grep -v grep|cut -c 10-14)
chmod 755 mykill
后台运行3个sleep进程:
#sleep 10000&
#sleep 10000&
#sleep 10000&
运行带参数的脚本:
#mykill sleep
[3] + Terminated sleep 10000&
[2] + Terminated sleep 10000&
[1] + Terminated sleep 10000&
zzl1997 回复于:2003-09-03 11:40:23我也来写一个.
#!/usr/bin/sh
echo ”Which process do you wantto kill:“
read P
ps -ef |grep ”$P“
echo
echo ”Please chioces the process do you want to killed:“
read K
kill -9 $K
这个对于前后台作业全可以.
我先前写了一个脚本,如下:
#!/usr/bin/sh
#The sprict is used to kill the process
echo ”Which process do you want to kill:“
read NAME
A=`ps -ef |grep ”$NAME“ |cut -b 10-14 |wc -l`
if [ $A = 1 ]
then
kill -9 $A
else
if [ $A -gt 1 ]
then
echo ”`$A >/zzl1997;more /zzl1997`“
else
echo ”`$A 2>/zzl1997;more /zzl1997`“
fi
我发现每次执行这个脚本,A就会产生一个与新进程,使得NAME得进程数肯定不为1,执行总是不能成功,
所以写了那个简单得,不会很傻吧!
zzl1997 回复于:2003-09-15 20:07:38大家为何不继续了呢?
Minsic 回复于:2003-09-16 12:06:06[quote:c0ffbbfa5f=”williamCU“]楼上这么复杂?
kill $(ps -ef|grep myproc|grep -v grep|cut -c 10-14)
以上kill一个进程名为myproc的进程。[/quote:c0ffbbfa5f]
人用的shell要是不支持$这样的写法呢?
williamCU 回复于:2003-09-16 12:56:37[quote:6bbb22ded7=”Minsic“]
人用的shell要是不支持$()这样的写法呢?[/quote:6bbb22ded7]
有道理
C shell好像不支持.
Minsic 回复于:2003-09-16 15:11:08[quote:295b4ee2f9=”albert“]
如果还有一个进程叫myproc1呢? 呵呵。[/quote:295b4ee2f9]
ps -ef|awk '$9~/^myproc$/{print $2}'
这样应该可以从一定程度上避免这种现象,不过如果myproc本身是由其他进程来调用的话,那另当别论了,或许$9应该换成$10?
大头虎 回复于:2003-11-14 20:18:49ps -ef|grep $Name|grep -v grep|awk '$NF==”$Name“ {print ”kill -9 “$2}'|/bin/sh
原文转自:www.ltesting.net
篇8:如何用shell编写一个不断PING其他机器,确认网络畅通的程序?Windows系统
如何用SHELL脚本编写一个程序,可以不断PING其他机器,如间隔3秒,以确认网络畅通,并把结果写到一个临时文件里。
lxlab 回复于:2004-12-29 21:23:09vi ping.net
ping -i 3 10.0.0.1 > temp.txt
gzxgzx 回复于:2005-01-05 09:13:39[quote:bf1b573197=”lxlab\"]vi ping_net
ping -i 3 10.0.0.1 > temp.txt[/quote:bf1b573197]
谢谢
lxlab 回复于:2005-01-05 18:45:14呵呵,不客气了!
原文转自:www.ltesting.net
★ 编写一个寓言故事
★ Linux服务器安全初始化Shell脚本linux服务器应用
★ 一个分页存储过程代码
★ 脚本范文
★ Unix系列shell程序编写(上)转自yesky.comWindows系统
★ 一个细节可以改变一切
★ 脚本写作范文
★ 汽车广告脚本范文
★ 个人简历编写
★ 如何编写大事记
关于shell脚本编写代码格式的一个细节(精选8篇)
欢迎下载DOC格式的关于shell脚本编写代码格式的一个细节,但愿能给您带来参考作用!