【问题标题】:ShellScript Issue Linux [closed]Linux中的Shell脚本[关闭]
【发布时间】:2015-07-09 09:30:45
【问题描述】:

任务 1:

如果时间是上午,则打印“现在是早上”。如果是下午,打印“现在不是早上”。

任务 2:

给定 2 个传递给脚本的参数。

验证是否已提交 2 个参数。如果未提供三个参数,则打印“必须提供 2 个参数”并退出脚本

  • 参数 1:应该是一个目录(需要验证)。如果不存在,打印“目录:目录,不存在”并退出脚本
  • 参数 2:应该是一个文件(需要验证)。如果不存在,打印“文件:文件名,不存在”并退出脚本。

如果所有参数都有效,则打印“Given valid: filename and directory”

这是我目前所拥有的

echo "James DuBois: 555555 - Task 1"
TIME=$(date "+%H")
if [ $TIME -lt 12 ]; then
    echo "morning"
else
    echo "not morning"
fi

echo "Task 2"

[ -d "$1" ] || exit
[ -d "$2" ] || exit
[ $# == 2 ] || exit

echo "arg1: $1"
echo "arg2: $2"

【问题讨论】:

  • 你的问题是什么?
  • 我正试图围绕任务 2 进行思考。我不知道参数如何与 linux 一起工作。目前只是在谷歌上搜索一些代码。
  • 为什么hours >= 12 会是早上?也许-lt 12 会更有意义?对于任务 2,相同类型的测试,但您需要条件表达式来测试文件,可能是-f?为我这样做完全正确man bash,然后/CONDITIONAL\ EXPRESSIONS 让我知道如果这不能解决你的问题。
  • 我认为我走在正确的轨道上?老实说,我不太清楚发生了什么。
  • StackOverflow 是一个很好的资源,当您隔离了一个问题并需要为其找到答案时。听起来您在这里没有孤立一个问题。程序规范与您在实施该规范时遇到的特定问题之间存在很大差距。两个独立的程序规范更不是一个孤立的问题。

标签: linux bash shell


【解决方案1】:

task 2中做同样的事情时,这会有一些额外的好处:

echo "Task 2"

[[ -d $1 && ! -L $1 && -f $2 && ! -L $2 ]] || exit
printf "%s\n" "arg1: $1" "arg2: $2"

它使用-L 选项检查符号链接。

[[ ]][ ] 有一些优势,比如处理空格而不需要引用变量。

如果您想在检查目录和文件时打印消息:

[[ ! -d $1 || -L $1 ]] && echo "Directory doesn't exist" && exit
[[ ! -f $2 || -L $2 ]] && echo "File doesn't exist" && exit
printf "%s\n" "arg1: $1" "arg2: $2"

【讨论】:

    【解决方案2】:

    James,BASH 是一个美妙而灵活的外壳。它有它的缺点,但如果你需要做任何与 Linux 系统管理等有关的事情,你可以在 bash 中完成。您的任务是让您熟悉使用条件表达式(测试)。几乎所有你需要的东西都有测试。这就是为什么我向您指出man bash条件表达式部分。

    您的第二个任务需要输入filename,以便您对其进行测试。我认为它打算作为参数传递给您的脚本(称为位置参数)。这是进行测试的一种方法。 注意:为了您的利益,我有意互换了输出例程 echoprintfprintf 更健壮一些)。看看以下内容,让我知道您有什么问题:

    #!/bin/bash
    # My first script
    #
    #  echo & printf are used at random below -- intentionally
    #
    
    [ -z $1 ] && {  # validate at least 1 argument given on command line
        printf "error: insufficient input. usage:  %s filename\n" "${0##*/}"
        exit 1
    }
    
    printf "\nJames DuBois: 5555555\n\n  Task 1\n\n"
    TIME=$(date "+%H")
    
    ## test for time of date: morning/not morning
    if [ $TIME -lt 12 ]; then
        printf " morning - time for coffee\n"
    else
        echo " not morning - time for scotch"
    fi
    
    echo -e "\n  Task 2\n"
    
    printf "Testing whether '%s' is a valid file.\n\n" "$1"
    
    ## test for file using compound commands
    [ -f "$1" ] && echo -e " file found: '$1'\n" || printf " file not found: '%s'\n\n" "$1"
    
    echo -e "Second test whether '$1' is a valid file.\n"
    
    ## test for file using if; then; else; fi
    if [ -f "$1" ]; then
        printf " file found: '%s'\n\n" "$1"
    else
        echo -e " file not found: '$1'\n"
    fi
    
    exit 0
    

    使用/输出

    $ bash ~/scr/tmp/stack/morningfile.sh
    error: insufficient input. usage:  morningfile.sh filename
    
    $ bash ~/scr/tmp/stack/morningfile.sh mtrx_simple_dyn.c
    
    James DuBois: 5555555
    
      Task 1
    
     not morning - time for scotch
    
      Task 2
    
    Testing whether 'mtrx_simple_dyn.c' is a valid file.
    
     file found: 'mtrx_simple_dyn.c'
    
    Second test whether 'mtrx_simple_dyn.c' is a valid file.
    
     file found: 'mtrx_simple_dyn.c'
    
    $ bash ~/scr/tmp/stack/morningfile.sh dog.c
    
    James DuBois: 5555555
    
      Task 1
    
     not morning - time for scotch
    
      Task 2
    
    Testing whether 'dog.c' is a valid file.
    
     file not found: 'dog.c'
    
    Second test whether 'dog.c' is a valid file.
    
     file not found: 'dog.c'
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-03-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-12
      • 1970-01-01
      • 2020-01-04
      • 2022-08-18
      相关资源
      最近更新 更多