gpt4 book ai didi

linux - Linux ShellScript 问题

转载 作者:太空宇宙 更新时间:2023-11-04 06:05:45 25 4
gpt4 key购买 nike

Task 1:

If the Time is AM, print "It is Morning". If it is PM, print "It is not morning".

Task 2:

Given 2 arguments passed to the script.

Validate that 2 arguments have been submitted. If three arguments have not been provided, print "Must Supply 2 Arguments" and exit the script

  • Argument 1: Should be a directory (needs validated). If this does not exist, print "Directory: directory, does not exist" and exit the script
  • Argument 2: Should be a file (needs validated). If this does not exist, print "File: filename, does not exist" and exit the script.

If all arguments are valid, print "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"

最佳答案

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

您的第二个任务需要输入文件名,以便您可以对其进行测试。我认为它旨在作为参数传递给您的脚本(称为位置参数)。这是进行测试的一种方法。 注意:为了您的利益,我特意交换了输出例程echoprintf(printf更加健壮)。请查看以下内容,让我知道您有什么问题:

#!/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'

关于linux - Linux ShellScript 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29954870/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com