You are not logged in.

#1 2022-09-11 20:55:33

Tx86
Banned
Registered: 2021-07-24
Posts: 39

Bash variable help [SOLVED]

Writing a script that mounts partitions. This is mainly being used as a way to mount and unmount USB sticks.

#!/bin/bash

 # USB Mounting and File Management Tool

 # Updated: 10-09-22, Version 1.0

 # You must be a sudoer or root to use this script


   ### Table of Contents ###

   # Line   Beginning Prompt and Command Menu
   # Line   Set Target Prompt
   # Line   Set Mount Path Prompt
   # Line   Mount/Unmount Filesystem (USB) Menu
   # Line   Change Directory Prompt
   # Line   View Prompt
   # Line   Copy Files To/From Filesystem (USB) Menu
   # Line   Delete Files from Filesystem (USB) Prompt
   # Line   Start of script when executed


  ### Beginning Prompt and Command Menu ###

BeginningPrompt () {
clear; echo; echo "USB Mounting and File Management Tool"; CommandMenu
}

CommandMenu () {
PS3="Option: "
PathStatus; echo "Please select an option."; echo; \
select Command in "View Available Filesystems" "Set Target Filesystem (USB)" "Set Mount Path" \
"Mount/Unmount Filesystem (USB)" "Change Directory" "View Files in Current Directory" \
"Copy Files To/From Filesystem (USB)" "Delete Files from Filesystem (USB)" "Exit"
do
case $Command in
"View Available Filesystems") clear; echo; sudo fdisk -l; echo | less -X; clear; CommandMenu;;
"Set Target Filesystem (USB)") clear; SetTargetPrompt;;
"Set Mount Path") clear; SetMountPathPrompt;;
"Mount/Unmount Filesystem (USB)") clear; MountUnmountMenu;;
"Change Directory") clear; ChangeDirectoryPrompt;;
"View Files in Current Directory") clear; ViewPrompt; CommandMenu;;
"Copy Files To/From Filesystem (USB)") clear; ToFromMenu;;
"Delete Files from Filesystem (USB)") clear; DeleteFilesPrompt;;
"Exit") echo; echo "Goodbye."; echo; exit;;
*) clear; CommandMenu;;
esac
done
}

PathStatus () {
echo; echo "Current Directory: $(pwd)" ; echo
if [ -z ${Dev} ] || [ ${Dev} = "0" ];
then echo "Target Filesystem (USB): Unset"
else echo "Target Filesystem (USB): $(echo "$Dev")"
fi

if [ -z ${MountPath} ] || [ ${MountPath} = "0" ];
then echo "Mount Path: Unset"; echo
else echo "Mount Path: $(echo "$MountPath")"; echo
fi
}


  ### Set Target Prompt ###

SetTargetPrompt () {
echo; echo "Enter 0 to cancel."; echo; read -p "Target Filesystem (USB): " Dev
if [ "$Dev" = "0" ];
then clear; CommandMenu
elif [ "$Dev" = "/dev/sda" ] || [ "$Dev" = */dev/sda* ];
then clear; echo; echo "/dev/sda is the not allowed to be used for this script due to safety risks."; echo | less -X; clear; SetTargetPrompt
elif [ -a "$Dev" ] && [ -z ${MountPath} ];
then clear; echo; echo "Target Filesystem successfully set as $(echo "$Dev")"; echo | less -X; clear; SetMountPathPrompt
elif [ -a "$Dev" ] && [ ! -z ${MountPath} ];
then clear; echo; echo "Target Filesystem successfully set as $(echo "$Dev")"; echo | less -X; clear; CommandMenu
else clear; echo; echo "Error: $(echo "$Dev") does not exist."; echo | less -X; clear; SetTargetPrompt
fi
}


  ### Set Mount Path Prompt ###

SetMountPathPrompt () {
echo; echo "Enter 0 to cancel."; echo; read -p "Set Mount Path: " MountPath
if [ "$MountPath" = "0" ];
then clear; CommandMenu
elif [ -a "$MountPath" ] && [ -z ${Dev} ];
then clear; echo; echo "Mount Path successfully set as $(echo "$MountPath")"; echo | less -X; clear; SetTargetPrompt
elif [ -a "$MountPath" ] && [ ! -z ${Dev} ];
then clear; echo; echo "Mount Path successfully set as $(echo "$MountPath")"; echo | less -X; clear; CommandMenu
else clear; echo; echo "Error: $(echo "$MountPath") does not exist."; echo | less -X; clear; SetMountPathPrompt
fi
}


  ### Mount/Unmount Filesystem (USB) Menu ###

MountUnmountMenu () {
PS3="Option: "
clear; PathStatus; echo "Please select an option."; echo; \
select MountUnmount in "Mount Filesystem (USB)" "Unmount Filesystem (USB)" "Cancel"
do
case $MountUnmount in
"Mount Filesystem (USB)") Mount;;
"Unmount Filesystem (USB)") Unmount;;
"Cancel") clear; CommandMenu;;
*) clear; MountUnmountMenu;;
esac
done
}

Mount () {
if [ -z ${Dev} ];
then clear; echo; echo "Error: Target Filesystem (USB) is not set."; echo | less -X; clear; SetTargetPrompt
elif [ -z ${MountPath} ];
then clear; echo; echo "Error: Mount Path is not set."; echo | less -X; clear; SetMountPathPrompt
else clear; echo; sudo mount "$Dev" "$MountPath"
  if [ "$?" -ne "0" ];
  then clear; echo; echo ""$Dev" was not successfully mounted."; echo | less -X; clear; CommandMenu
  else clear; echo; echo ""$Dev" was successfully mounted to "$MountPath.""; echo | less -X; clear; CommandMenu
  fi
fi
}

Unmount () {
if [ -z ${Dev} ];
then clear; echo; echo "Error: Target Filesystem (USB) is not set."; echo | less -X; clear; SetTargetPrompt
else clear; echo; sudo umount "$Dev"
  if [ "$?" -ne "0" ];
  then clear; echo; echo ""$Dev" was not successfully unmounted."; echo | less -X; clear; CommandMenu
  else clear; echo; echo ""$Dev" was successfully unmounted from "$MountPath.""; echo | less -X; clear; CommandMenu
  fi
fi
}


  ### Change Directory Prompt ###

ChangeDirectoryPrompt () {
clear; echo; echo "Current Directory: $(pwd)"; echo; echo;\
echo "Enter 0 to cancel."; echo; read -p "Full Directory Path: " DirectoryChange
if [ "$DirectoryChange" = "0" ];
then clear; CommandMenu
elif [ -d "$DirectoryChange" ];
then cd "$DirectoryChange"; clear; CommandMenu
else clear; echo; echo "Error: $(echo "$DirectoryChange") does not exist."; echo | less -X; clear; ChangeDirectoryPrompt
fi
}


  ### View Prompt ###

ViewPrompt () {
echo; echo "Please select a view mode."; echo
echo "1) Simple View"
echo "2) Detailed View"
echo "Enter 0 to cancel command."; echo
read -p "Option: " ViewOption
if [ "$ViewOption" -eq "1" ];
then clear; echo; echo "Files:"; echo "******"; echo; ls -Ax; echo
elif [ "$ViewOption" -eq "2" ];
then clear; ls -Ax; echo | less -X
elif [ "$ViewOption" = "0" ];
then clear; CommandMenu
else clear; ViewPrompt
fi
}


  ### Copy Files To/From Filesystem (USB) Menu ###

ToFromMenu () {
PS3="Option: "
PathStatus; echo "Please select an option."; echo; \
select ToFrom in "View files on Filesystem (USB)" "Copy files to Filesystem (USB)" "Copy files from Filesystem (USB)" \
"Copy all files in current directory to Filesystem (USB)" "Copy all files from Filesystem (USB) to current directory" "Cancel"
do
case $ToFrom in
"View files on Filesystem (USB)") clear; ViewPromptForTargetFilesystem; ToFromMenu;;
"Copy files to Filesystem (USB)") clear; CopyFilesTo;;
"Copy files from Filesystem (USB)") clear; CopyFilesFrom;;
"Copy all files in current directory to Filesystem (USB)") clear; CopyAllFilesTo;;
"Copy all files from Filesystem (USB) to current directory") clear; CopyAllFilesFrom;;
"Cancel") clear; CommandMenu;;
*) clear; ToFromMenu;;
esac
done
}

PathCheckForCopying () {
if [ -z ${MountPath} ];
then clear; echo; echo "Error: Mount Path is not set."; echo | less -X; clear; SetMountPathPrompt
fi
}

ViewPromptForTargetFilesystem () {
PathCheckForCopying
echo; echo "Please select a view mode."; echo
echo "1) Simple View"
echo "2) Detailed View"
echo "Enter 0 to cancel command."; echo
read -p "Option: " ViewOption
if [ "$ViewOption" -eq "1" ];
then clear; echo; echo "Files:"; echo "******"; echo; ls -Ax "$MountPath"; echo
elif [ "$ViewOption" -eq "2" ];
then clear; ls -Ax "$MountPath"; echo | less -X
elif [ "$ViewOption" = "0" ];
then clear; ToFromMenu
else clear; ViewPromptForTargetFilesystem
fi
}

CopyFilesTo () {
PathCheckForCopying
ViewPrompt
echo "Enter 0 to cancel command."; echo
read -p "File to copy: " CopyFileTo
if [ -a "$CopyFileTo" ];
then sudo cp -r "$CopyFileTo" "$MountPath"; clear; echo; echo "Files in "$MountPath": \
$(ls -Ax "$MountPath")"; echo | less -X; clear; ToFromMenu
elif [ "$CopyFileTo" = "0" ];
then ToFromMenu
else clear; echo; ls "$CopyFileTo"; echo | less -X; clear; CopyFilesTo
fi
}

CopyAllFilesTo () {
PathCheckForCopying
sudo cp -r * "$MountPath"; clear; echo; echo "Files in "$MountPath": \
$(ls -Ax "$MountPath")"; echo | less -X; ToFromMenu
}

CopyFilesFrom () {
PathCheckForCopying
ViewPromptForTargetFileSystem
echo "Enter 0 to cancel command."; echo
read -p "File to copy: " CopyFileFrom
if [ -a "$CopyFileFrom" ];
then sudo cp -r "$MountPath"/"$CopyFileFrom" .; clear; echo; echo "Files in "$MountPath": \
$(ls -Ax "$MountPath")"; echo | less -X; ToFromMenu
elif [ "$CopyFileFrom" = "0" ];
then ToFromMenu
else clear; echo; ls "$CopyFileFrom"; echo | less -X; clear; CopyFilesFrom
fi
}

CopyAllFilesFrom () {
PathCheckForCopying
sudo cp -r "$MountPath"/* .; clear; echo; echo "Files in "$MountPath": \
$(ls -Ax "$MountPath")"; echo | less -X; ToFromMenu
}


  ### Delete Files from Filesystem (USB) Prompt ###

PathCheckForDeletion () {
if [ -z ${MountPath} ];
then clear; echo; echo "Error: Mount Path is not set."; echo | less -X; clear; SetMountPathPrompt
fi
}

ViewPromptForDeletion () {
echo; echo "Please select a view mode."; echo
echo "1) Simple View"
echo "2) Detailed View"
echo "Enter 0 to cancel command."; echo
read -p "Option: " ViewOption
if [ "$ViewOption" -eq "1" ];
then clear; echo; echo "Files:"; echo "******"; echo; ls -Ax "$MountPath"; echo
elif [ "$ViewOption" -eq "2" ];
then clear; ls -Ax "$MountPath"; echo | less -X
elif [ "$ViewOption" = "0" ];
then clear; CommandMenu
else clear; ViewPromptForDeletion
fi
}

DeleteFilesPrompt () {
PathCheckForDeletion
ViewPromptForDeletion
echo "Warning: Deletion is permanent and irreversible."
echo "Enter 0 to cancel command. Enter 000 to delete all files."; echo
read -p "Entry for deletion: " DeleteFile
if [ -a "$DeleteFile" ];
then echo; DeleteConfirmationMenu
elif [ "$DeleteFile" = "0" ];
then clear; CommandMenu
elif [ "$DeleteFile" = "000" ];
then echo; DeleteAllConfirmationMenu
else clear; echo; echo "File does not exist."; echo | less -X; clear; DeleteFilesPrompt
fi
}

DeleteConfirmationMenu () {
PS3="Option: "
echo "Confirm to delete file: "$DeleteFile""; echo; select Delete in "Yes" "No" "Cancel"
do
case $Delete in
"Yes") sudo rm -R "$DeleteFile" "$MountPath"; clear; echo; echo ""$DeleteFile" has been deleted."; echo | less -X; clear; DeleteFilesPrompt;;
"No") clear; DeleteFilesPrompt;;
"Cancel") clear; CommandMenu;;
*) clear; DeleteConfirmationMenu;;
esac
done
}

DeleteAllConfirmationMenu () {
PS3="Option: "
echo "Confirm to delete all files on: "$Dev""; echo; select DeleteAll in "Yes" "No" "Cancel"
do
case $DeleteAll in
"Yes") sudo rm -R "$MountPath"/*; clear; echo; echo "All files have been deleted from "$MountPath"."; echo | less -X; \
clear; CommandMenu;;
"No") clear; DeleteFilesPrompt;;
"Cancel") clear; CommandMenu;;
*) clear; DeleteAllConfirmationMenu;;
esac
done
}


  ### Start of script when executed ###

What's giving me trouble is this:

elif [ "$Dev" = "/dev/sda" ] || [ "$Dev" = */dev/sda* ];
then clear; echo; echo "/dev/sda is the not allowed to be used for this script due to safety risks."; echo | less -X; clear; SetTargetPrompt

I'm trying to make it to where if anything involving /dev/sda in inputted at all, the script will not allow it. Now, I can get it to where if "/dev/sda" is specifically entered, it does what I want. But obviously, I need it to do the same thing for /dev/sda1, sda2, and so on.

So how do I get it to where any input containing "sda", regardless of what comes before or after, is considered verboten?

Any help in this matter would be greatly appreciated.

Last edited by Tx86 (2022-09-14 04:39:13)

Offline

#2 2022-09-11 21:04:31

Slithery
Administrator
From: Norfolk, UK
Registered: 2013-12-01
Posts: 5,776

Re: Bash variable help [SOLVED]

Tx86 wrote:

I'm trying to make it to where if anything involving /dev/sda in inputted at all, the script will not allow it.

Why? It's never guaranteed that sdaX will be the system drive, it could easily be a USB device (especially on systems with an NVME device as its only internal drive).
For more information read the introductory paragraph of...
https://wiki.archlinux.org/title/Persis … ice_naming


No, it didn't "fix" anything. It just shifted the brokeness one space to the right. - jasonwryan
Closing -- for deletion; Banning -- for muppetry. - jasonwryan

aur - dotfiles

Offline

#3 2022-09-11 21:10:42

GaKu999
Member
From: US/Eastern
Registered: 2020-06-21
Posts: 696

Re: Bash variable help [SOLVED]

Eh, why not use https://wiki.archlinux.org/title/Udisks?

Specifically the Mount Helpers like udiskie are really useful for making one's life easier, an alias is enough.

I mean, if there's a particular reason then do whatever floats your boat I guess...


My reposSome snippets

Heisenberg might have been here.

Offline

#4 2022-09-11 21:32:11

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,452
Website

Re: Bash variable help [SOLVED]

First, read the points above.  This is a bad idea.  Second, reread the points above, because this is a really bad idea.  But if you just want to learn about scripting and get a direct response to this:

Tx86 wrote:

I'm trying to make it to where if anything involving /dev/sda in inputted at all, the script will not allow it.

Then use grep:

if echo $Dev | grep -q '/sda'; then
   echo "NO SOUP FOR YOU!"
   exit 1
fi

Sidenote: if you want anyone to read your code, please indent nested lines.


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Online

#5 2022-09-11 22:10:52

2ManyDogs
Forum Moderator
Registered: 2012-01-15
Posts: 4,645

Re: Bash variable help [SOLVED]

Without discussing the wisdom of what you are doing, in bash you can do this:

if [[ $Dev =~ "sda" ]]; then

or this:

if [[ $Dev == *"sda"* ]]; then

Both determine if $Dev contains the string "sda"


How to post. A sincere effort to use modest and proper language and grammar is a sign of respect toward the community.

Offline

#6 2022-09-12 08:45:21

ayekat
Member
Registered: 2011-01-17
Posts: 1,589

Re: Bash variable help [SOLVED]

Or if you want that footgun to be POSIX-compatible (yes yes, we don't care because it's Bash, but for completeness' sake):

case "$Dev" in (*sda*)
    # do thing here
esac

pkgshackscfgblag

Offline

#7 2022-09-14 04:38:31

Tx86
Banned
Registered: 2021-07-24
Posts: 39

Re: Bash variable help [SOLVED]

@2ManyDogs

Thank you for the answer, that's exactly what I needed.

Offline

Board footer

Powered by FluxBB