【发布时间】:2022-01-11 10:47:17
【问题描述】:
我想创建一个以长格式列出目录内容的脚本。但是,我想首先确保用户在提示之前不会将输入添加到脚本命令行,如果需要应用错误并退出。如果它通过了该测试,那么脚本应该在尝试列出它之前确保该目录存在。如果没有,则出现另一个错误。如果存在,我需要在长列表中显示内容。
我在这方面真的很陌生,这就是我目前所拥有的。我不确定我是否走在正确的轨道上。任何帮助将不胜感激。
#!/bin/bash
# File Name: dlist
# Usage: dlist prompt: [/directory/path/here]
# Synopsis: The dlist script will prompt the user to input a directory path and will
# return a long listing of the contents in that directory.
# Author: Romero
# Notify user of incoming prompt
echo -e "You will be prompted to enter the name of a directory you want to list in long form. "
# Prompt the user to input the directory to list on the same line as the input prompt
read -p "Directory to list: "
# Ensure User has not input directly into command line; if so issue std error 1 and exit
# Check to ensure directory exists before listing; if not issue std error 2 and exit
# If directory exists, list it in long form
if [ ! -d "$DIRECTORY" ]; then
echo "This directory does not exist to list!" 1>&2
exit 2
elif [ -d "$DIRECTORY" ]; then
echo ls -l "$DIRECTORY"
fi
【问题讨论】:
标签: linux bash input directory scripting