Search code examples
linuxbashshellscriptingflags

want to make multiple flags in script?


I want to make a multiple flags for my script. And didn't work like what I want it.

    #!/bin/bash

function wherecpy
{
    echo "Plaes write where the files are located: "
    read placefile
    cd $placefile
}

function funcpy
{
    wherecpy
    echo ""
    echo "Please choose the following:"
    echo ""
    echo "1. Interactive copy, answer yes or no (y/n) before doing the copy"
    echo "2. Make backups of existing destination files"
    echo "3. Preserve file attributes"
    echo "4. Do a recursively copy"
    echo "5. Back to main menu"
    echo ""
        echo -n "Enter Your Selection: "
        read selection
        echo ""
    case $selection in
        1) a="-i" ;;
        2) b="--backup" ;;
        3) c="--preserve=all" ;;
        4) d="-r" ;;
        0) return 0 ;;
    esac
    echo "Type the name of the file you wish to copy/backup: "
    read file
    echo "Type the name of the destination file/directory: "
    read dest
    cp $a $b $c $d "$file" "$dest"
    if [ $? == 0 ]; then
    echo "Success"
    else
    echo "failed."
    fi

}

I want it to work like that :

Please type in the source file(s) to copy: File1 file2 file3
Please type in the destination: dir1
Copy successful.
Press any key to return to main menu.


Solution

  • Use getopts :

    There are many online resources you can take reference from