Home > other >  how to rename directory using a menu
how to rename directory using a menu

Time:01-23

i am tring to create a menu that can rename a directory i am having an issue as i am using the mv command but im not sure how to ask for two different variables to then rename the directory

PS3="Please enter the number of what you would like to do: " 
menu=("Create Directory" "Rename a directory" "Delete a Directory" "Change Directory" "List directories" "exit menu")  
select opt in "${menu[@]}"  
do
    case $opt in
        "Create Directory")
            echo "What do you want to call the directory?" ; read ; mkdir $REPLY ; #works
            ;;
        
    "Rename a directory")
        echo "What directory would you like to rename?" ; read ; echo "What would you like to rename it to?" ; read ; mv $var1 $var2; 
        ;;
        
    "Delete a Directory")
        echo "what directory do you wan tto delete/" ; read ; rmdir $REPLY ; #works
        ;;
        
    "Change Directory")
        echo "What directory do you want to change to? " ; read ; cd $REPLY ; 
        ;;
        
    "List directories")
          ; 
        ;; 
        
    "exit menu")
        break  #works
        ;;
        
esac 

the rename the directory part is what i need help with.

CodePudding user response:

The below is the beginning of another script which performs command line parsing to fit your needs.

You can easily switch that up to work with manual input (multiple prompt read sequences) to gather the required data for the desired actions.

It shows that you can do nesting of case/condition matching.

The only other suggestion I would make is that you use the discipline of separating user-interaction code segments from action-performing code segments (as functions). That approach allows the flow logic to be viewed in concise form, keeping action-related complexity "encapsulated" in the function calls.

#!/bin/bash
doSample=1
delim=1
pagesFirstUse=0
pagesLastUse=0
doSplit=0
Ph_4_Mode=0
while [ $# -gt 0 ]
do
    case $1 in
        --sample ) doSample=1 ; shift ;;
        --full ) doSample=0 ; shift ;;
        --first ) pagesFirstUse=$2 ; doSample=0 ; shift ; shift ;;
        --last ) pagesLastUse=$2 ; doSample=0 ; shift ; shift ;;
        --mode )
            case $2 in
                first_line )
                    doSplit=1 ;
                    Ph_4_Mode=1 ;;
                last_line )
                    doSplit=1 ;
                    Ph_4_Mode=2 ;;
                * ) ;;
            esac
            shift ; shift ;;
        --nodelim ) delim=0 ; shift ;;
        * ) echo -e "\n Invalid parameter on the command line.  Only valid options: [ --sample | --full | --first | --last | --mode [first_line|last_line] | --nodelim ].\n Bye!\n" ; exit 1 ;;
    esac
done

CodePudding user response:

As you correctly understood, a read stores its answer to the variable REPLY. If you do 2 read in succession, the second one will of course overwrite this variable. As a solution, either save the first reply into a different variable, i.e.

read
reply1=$REPLY
read
# $reply1 and $REPLY give you the two replies

or provide a different variable name after the read:

read
read reply2
# $REPLY and $reply2 give you the two replies

Side note:

My recommendation would be to always provide an explicit variable name when using read. It is less error-prone and better for readability. Laziness is often a virtue, but not always!

  •  Tags:  
  • bash
  • Related