![이 스크립트가 수행하는 작업을 이해하는 데 도움이 필요합니다.](https://linux55.com/image/212015/%EC%9D%B4%20%EC%8A%A4%ED%81%AC%EB%A6%BD%ED%8A%B8%EA%B0%80%20%EC%88%98%ED%96%89%ED%95%98%EB%8A%94%20%EC%9E%91%EC%97%85%EC%9D%84%20%EC%9D%B4%ED%95%B4%ED%95%98%EB%8A%94%20%EB%8D%B0%20%EB%8F%84%EC%9B%80%EC%9D%B4%20%ED%95%84%EC%9A%94%ED%95%A9%EB%8B%88%EB%8B%A4..png)
if [ $# -ne 1 ]
then
echo Usage: $0 DIRECTORY
exit 1
elif [ ! -d $1 ]
then
echo $1 is not a directory
exit 2
fi
directory=$1
cd $directory
files=*
echo $directory
echo $files
exit 0
지금까지 그걸 깨려고 노력 중이었어
$#
남은 매개변수의 개수입니다.[
테스트 명령입니다-ne
숫자 "같지 않음" 연산자입니다.if [ $# -ne 1 ]
인수가 하나만 있는지 테스트하는 경우에도 마찬가지입니다(왼쪽).
두 번째 예에서는:
!
아니오를 의미-d
출력 디렉터리를 나타냅니다.$1
첫 번째 남은 매개변수입니다.
답변1
제공한 스크립트를 복사 shellcheck.com
하고 발견된 문제를 해결한 후:
- 실종된 소녀
- 누락된 인용문이 많음
- CD가 실패하면 스크립트도 실패하는지 확인하세요.
- 할당은 변수에 문자가 하나만 포함된다는 의미
files=*
와 동일합니다 . 전체 파일 목록을 .files="*"
*
files
스크립트는 다음과 같습니다.
#!/bin/sh -
if [ "$#" -ne 1 ] # check that there is one argument given.
then
echo "Usage: $0 DIRECTORY" # inform the user how to use the script.
exit 1
elif [ ! -d "$1" ] # check that the argument is a directory.
then
echo "$1 is not a directory" # inform the user if on error.
exit 2
fi
directory=$1
cd "$directory" || exit 3 # change to that directory
set -- * # get a list of files inside
files="$*"
echo "$directory" # Print the name
echo "$files" # Print the file list.
exit 0
여전히 더 높은 수준의 문제가 있지만 스크립트는 주석에 설명되어 있습니다.
전체 스크립트는 디렉터리의 파일을 나열합니다.