试图在我的shell脚本中实现CASE

时间:2018-03-23 20:49:49

标签: linux bash shell case

我试图在我的小安全删除脚本中添加选项。例如,我可以#!/bin/bash #Purpose = Safe delete #Created on 20-03-2018 #Version 0.8 #jesus,i'm dumb #START ##Constants## dir="/home/cunha/LIXO" #check to see if the imputs are a files# for input in "$@"; do if ! [ -e "$input" ]; then echo "Input is NOT a file!" exit 1 fi done ###main### case $1 in -r) echo "test option -r" ;; *) if [[ -f "$dir/$fwe.tar.bz2" ]]; then echo "File already exists." if [[ "$file" -nt "$2" ]]; then echo "Removing older file." && rm "$dir"/"$fwe.tar.bz2" && tar -czPf "$fwe.tar.bz2" "$(pwd)" && mv "$fwe.tar.bz2" "$d$ fi else echo "Ziping it and moving." && tar -czPf "$fwe.tar.bz2" "$(pwd)" && mv "$fwe.tar.bz2" "$dir" fi done ;; esac ,它将删除大小超过100 kbs的文件。无论如何,我的安全防护功能都有问题。

./sdell -r file1.txt

问题是当我拨打#!/bin/bash #Purpose = Safe delete #Created on 20-03-2018 #Version .7 #START ##Constants## dir="/home/cunha/LIXO" #check to see if the imputs are a files# for input in "$@"; do if ! [ -e "$input" ]; then echo "Input is NOT a file!" exit 0 fi done ###main### ##Cycle FOR so the script accepts multiple file inputs## for file in "$@"; do fwe="${file%.*}" #IF the input file already exist in LIXO# if [[ -f "$dir/$fwe.tar.bz2" ]]; then echo "File already exists." #IF the input file is newer than the file thats already in LIXO# if [[ "$file" -nt "$2" ]]; then echo "Removing older file." && rm "$dir"/"$fwe.tar.bz2" && tar -czPf "$fwe.tar.bz2" "$(pwd)" && mv "$fwe.tar.bz2" "$d$ fi else echo "Ziping it and moving." && tar -czPf "$fwe.tar.bz2" "$(pwd)" && mv "$fwe.tar.bz2" "$dir" fi done 时,它说输入不是文件。

这是没有案例的脚本,在选择之前100%正常工作。

var cellFormula = "=SUM(A1*B1)";       
theNewSheet.range("D1:D9").formula(cellFormula);

2 个答案:

答案 0 :(得分:3)

您看到的消息与案例无关.......构建,由本节打印:

for input in "$@"; do
if ! [ -e "$input" ]; then
    echo "Input is NOT a file!"
    exit 1
fi
done

扩展所有命令行参数($ @),包括“-r”,并退出脚本,因为“-r”不是文件。案件......永远不会到达。您可以使用

运行脚本
bash -x file.sh -r test

这样您就可以确切地看到正在执行的行。

下面的代码段可能会按照您的意愿执行,按顺序处理所有参数:

#!/bin/bash

while [ ! -z $1 ]; do
    case $1 in
        -r) echo "option R"
            ;;
        -f) echo "option F"
            ;;
         *) if [ -f $1 ]; then echo "$1 is a file." ; fi
            ;;
    esac
    shift
done

答案 1 :(得分:1)

在尝试其他选项之前,请考虑检查是否已通过-r,如果是{{p>}则使用shift

#!/usr/bin/env sh

dir="/home/cunha/LIXO"

case $1 in
    -r)     echo "test option -r"
            shift
            ;;
esac

#check to see if the imputs are a files#
for input in "$@"; do
    echo current input: "$input"
    if ! [ -e "$input" ]; then
        echo "Input $input is NOT a file!"
        exit 1
    fi
done

if [[ -f "$dir/$fwe.tar.bz2" ]]; then
    echo "File already exists."
    if [[ "$file" -nt "$2" ]]; then
        echo "Removing older file..."
        # add stuff
    fi
else
    echo "Ziping it and moving."
    # add stuff
fi