Przejdź do treści

BASH – Skrypt Montowania I odmontowywania share

#!/bin/bash

# Check if the user is root
if [ "$EUID" -ne 0 ]; then
  echo "Script must be run as root. Run it with sudo."
  exit 1
fi

# Check if the target directory already exists
mount_point="/mnt/share"
if [ ! -d "$mount_point" ]; then
  echo "Creating target directory: $mount_point"
  mkdir -p "$mount_point"
fi

# Function to mount the network share
mount_share() {
  echo "Mounting //share/ to $mount_point"
  mount -t cifs -o username=yourUsername,password=yourPassword //share/ "$mount_point"
  
  # Check if the mounting was successful
  if [ $? -eq 0 ]; then
    echo "Mounting successful."
  else
    echo "Error during mounting. Check if you provided correct login credentials."
    exit 1
  fi
}

# Function to unmount the network share
umount_share() {
  echo "Unmounting network share from $mount_point"
  umount "$mount_point"

  # Check if unmounting was successful
  if [ $? -eq 0 ]; then
    echo "Unmounting successful."
  else
    echo "Error during unmounting."
    exit 1
  fi
}

# Check if the script was run with the "umount" argument
if [ "$1" == "umount" ]; then
  umount_share
else
  mount_share
fi

Dodaj komentarz

Twój adres email nie zostanie opublikowany. Wymagane pola są oznaczone *