LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   want to make multiple flags in script? (https://www.linuxquestions.org/questions/linux-newbie-8/want-to-make-multiple-flags-in-script-4175525508/)

stetas 11-15-2014 04:04 PM

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.

Code:

  #!/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 :

Quote:

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.
thanks!

Teufel 11-15-2014 11:54 PM

1. Bash interpretor will not run into your functions directly. As you have it right now, the interpretor bypasses your functions.
You have to invoke your function explicitely from the script. Add wherecpy at the end of your script.

2. As you have it right now, only one parameter will be added to 'cp' command.
You need something like this in funcpy function:
Code:

<here the user input>
flags=""
if [[ $selection = *1* ]]
then
    flag="$flag -i"
fi
if [[ $selection = *2* ]]
then
    flag="$flag --backup"
fi
if [[ $selection = *3* ]]
then
    flag="$flag --preserve=all"
fi
if [[ $selection = *4* ]]
then
    flag="$flag -r"
fi
if [[ $selection = *5* ]]
then
    return 0
fi
...
...
...
cp $flag "$file" "$dest"


stetas 11-16-2014 07:36 AM

I did it like this way

Code:

#!/bin/bash



flag1="." #variables that checks if the flag already exists
flag2="."
flag3="."
flag4="."
flags="" #variable that contains all variables

function funcpy
{
putin=
until        [ "$putin" = "0" ]; do
        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. Zero to flag"
        echo "6. Back to main menu"
        echo ""
        echo -n "Enter Your Selection: "
        read putin
        echo ""
        case $putin in
                1) if [ $flag1 == "." ]; then #checking if the flag is there, and then put it
                flag1="-" #to this
                flags="$flags -i"
                echo $flags
                fi
                ;;
                2) if [ $flag2 == "." ]; then
                flag2="-"
                flags="$flags --backup"
                echo $flags
                fi
                ;;
                3) if [ $flag3 == "." ]; then
                flag3="-"
                flags="$flags --preserve=all"
                echo $flags
                fi
                ;;
                4) if [ $flag4 == "." ]; then
                flag4="-"
                flags="$flags --recursive"
                echo $flags
                fi
                ;;
                5) clear #to rest the flags (but not working :/ )
                flag1="."
                flag2="."
                flag3="."
                flag4="."
                flags""
                funcpy
                ;;
                6) show_menu ;;
                *)
                ;;
        esac
               
                echo "Please type in the source file to copy: "
                read filee
                echo "Please type in the destination: "
                read destinatioon
                cp $flags $filee $destinatioon
                if [ $? == 0 ]; then
                echo "Success"
                press_enter
               
                else
                echo "Failed"
                press_enter
               
                fi
               
done
}

function press_enter
{
        echo ""
        echo -n "Press (Enter) To Continue"
        read
        clear
}



Quote:

Please type in the source file to copy:
/home/user1/Music/doc1 /home/user1/Music/doc2
Please type in the destination:
/home/user1/Desktop/
Success

Press (Enter) to continue
And its work now :D

How I can make the flags rest ? I did one but didn't work.

pan64 11-16-2014 10:13 AM

what does "How I can make the flags rest ? I did one but didn't work.' this sentence mean?
Probably you missed a = here:
Code:

                5) clear #to rest the flags (but not working :/ )
                flag1="."
                flag2="."
                flag3="."
                flag4="."
                flags=""
                funcpy
                ;;


stetas 11-16-2014 10:54 AM

thanks!


All times are GMT -5. The time now is 10:09 AM.