LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-15-2014, 05:04 PM   #1
stetas
LQ Newbie
 
Registered: Dec 2013
Posts: 16

Rep: Reputation: Disabled
Exclamation 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!

Last edited by stetas; 11-16-2014 at 11:54 AM.
 
Old 11-16-2014, 12:54 AM   #2
Teufel
Member
 
Registered: Apr 2012
Distribution: Gentoo
Posts: 510

Rep: Reputation: 120Reputation: 120
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"
 
1 members found this post helpful.
Old 11-16-2014, 08:36 AM   #3
stetas
LQ Newbie
 
Registered: Dec 2013
Posts: 16

Original Poster
Rep: Reputation: Disabled
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

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

Last edited by stetas; 11-16-2014 at 10:15 AM.
 
Old 11-16-2014, 11:13 AM   #4
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 8,110

Rep: Reputation: 2268Reputation: 2268Reputation: 2268Reputation: 2268Reputation: 2268Reputation: 2268Reputation: 2268Reputation: 2268Reputation: 2268Reputation: 2268Reputation: 2268
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
		;;
 
1 members found this post helpful.
Old 11-16-2014, 11:54 AM   #5
stetas
LQ Newbie
 
Registered: Dec 2013
Posts: 16

Original Poster
Rep: Reputation: Disabled
thanks!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Chmod it's flags and how to use it to make a script executable Ztcoracat Linux - General 12 06-25-2014 10:22 PM
How to make multiple threads of currently running shell script? aarsh Linux - Newbie 10 04-21-2010 08:51 PM
How to add multiple flags in bash script manya Programming 10 10-08-2009 09:27 AM
multiple -d flags not allowed masimiqbal Linux - Newbie 2 05-06-2009 05:25 AM


All times are GMT -5. The time now is 12:02 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration