파일 시스템이 스크립트로 마운트되었는지 확인하는 방법 … 나는

나는 스크립팅에 익숙하지 않다 … 나는 매우 기본적인 것들을 할 수 있지만 이제는 손이 필요하다.

백업이 필요한 경우에만 마운트되는 로컬 파일 시스템이 있습니다.

나는 이것으로 시작합니다.

#!/bin/bash
export MOUNT=/myfilesystem

if grep -qs $MOUNT /proc/mounts; then
  echo "It's mounted."
else
  echo "It's not mounted."; then
  mount $MOUNT;
fi

내가 말했듯이, 나는 스크립팅에 매우 기본적입니다. mount리턴 코드를보고 명령 의 상태를 확인할 수 있다고 들었습니다 .

RETURN CODES
       mount has the following return codes (the bits can be ORed):
       0      success
       1      incorrect invocation or permissions
       2      system error (out of memory, cannot fork, no more loop devices)
       4      internal mount bug
       8      user interrupt
       16     problems writing or locking /etc/mtab
       32     mount failure
       64     some mount succeeded

나는 그것을 확인하는 방법을 모른다. 어떤 안내?



답변

mountshell special parameter를 사용하여 가장 잘 작성된 실행 파일 의 상태 코드를 확인할 수 있습니다 ?.

보낸 사람 man bash:

? Expands to the exit status of the most recently executed foreground pipeline.

mount명령 을 실행 한 후 즉시 실행 echo $?하면 이전 명령의 상태 코드가 인쇄됩니다.

# mount /dev/dvd1 /mnt
  mount: no medium found on /dev/sr0
# echo $?
  32

모든 실행 파일에 잘 정의 된 상태 코드가있는 것은 아닙니다. 최소한 성공 (0) 또는 실패 (1) 코드로 종료해야하지만 항상 그런 것은 아닙니다.

예제 스크립트를 확장하고 수정하기 if위해 명확성을 위해 중첩 구조를 추가했습니다 . 상태 코드를 테스트하고 작업을 수행하는 유일한 방법은 아니지만 학습 할 때 읽는 것이 가장 쉽습니다.

#!/bin/bash
mount="/myfilesystem"

if grep -qs "$mount" /proc/mounts; then
  echo "It's mounted."
else
  echo "It's not mounted."
  mount "$mount"
  if [ $? -eq 0 ]; then
   echo "Mount success!"
  else
   echo "Something went wrong with the mount..."
  fi
fi

“종료 및 종료 상태”에 대한 자세한 정보는 고급 Bash 스크립팅 안내서를 참조하십시오 .


답변

많은 Linux 배포판에 mountpoint명령이 있습니다. 디렉토리가 마운트 포인트인지 여부를 확인하는 데 명시 적으로 사용할 수 있습니다. 이처럼 간단합니다.

#!/bin/bash    
if mountpoint -q "$1"; then
    echo "$1 is a mountpoint"
else
    echo "$1 is not a mountpoint"
fi

답변

한 가지 더 방법 :

if findmnt ${mount_point}) >/dev/null 2>&1 ; then
  #Do something for positive result (exit 0)
else
  #Do something for negative result (exit 1)
fi

답변

루트가 필요하지 않은 가장 쉬운 방법은 다음과 같습니다.

if $(df | grep -q /mnt/ramdisk); then

fi

또는 마운트되어 있지 않은지 확인하십시오.

if ! $(df | grep -q /mnt/ramdisk); then

fi

답변

짧은 진술

장착되어 있는지 확인하십시오 .

mount|grep -q "/mnt/data" && echo "/mnt/data is mounted; I can follow my job!"

장착되어 있지 않은지 확인하십시오 .

mount|grep -q "/mnt/data" || echo "/mnt/data is not mounted I could probably mount it!"

답변

나는 아래 스크립트로 시도했다

#!/bin/bash
echo "enter the file system to check whether its mounted or not"
read p
echo $p
for i in `cat /proc/mounts`
do
if [[ $p =~ $i ]]
then
echo "$p is mounted"
else
echo "$p is not mounted"
fi
done

파일 시스템 이름 만 입력하면됩니다.