LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
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, 04: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 10:54 AM.
 
Old 11-15-2014, 11:54 PM   #2
Teufel
Member
 
Registered: Apr 2012
Distribution: Gentoo
Posts: 616

Rep: Reputation: 142Reputation: 142
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, 07: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 09:15 AM.
 
Old 11-16-2014, 10:13 AM   #4
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,850

Rep: Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309
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, 10:54 AM   #5
stetas
LQ Newbie
 
Registered: Dec 2013
Posts: 16

Original Poster
Rep: Reputation: Disabled
thanks!
 
  


Reply



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 09:22 PM
How to make multiple threads of currently running shell script? aarsh Linux - Newbie 10 04-21-2010 07:51 PM
How to add multiple flags in bash script manya Programming 10 10-08-2009 08:27 AM
multiple -d flags not allowed masimiqbal Linux - Newbie 2 05-06-2009 04:25 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 05:42 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
Open Source Consulting | Domain Registration