LinuxQuestions.org
Help answer threads with 0 replies.
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 12-24-2009, 09:47 PM   #1
isdigit
LQ Newbie
 
Registered: Dec 2009
Posts: 6

Rep: Reputation: 0
Returning Strings - Bash


Code:
#!/bin/bash

ActivateMonitoring() {

echo -n "Enter device: "
read Device
xterm -e ifconfig $Device down
xterm -e macchanger -A $Device
xterm -e ifconfig $Device up
xterm -e airmon-ng stop $Device
xterm -e airmon-ng start $Device

echo -n "Any more[y/n]? "
read Decision
if [[ $Decision == 'y' ]]; then
{
ActivateMonitoring
}
else
echo 
echo  "Monitoring mode Configured"
fi

return '$device'
}
s=""
ActivateMonitoring = s
echo $s


exit 0
I am trying to get the function ActivateMonitoring to return a string, so I can use a global variable and then in return, use it as input for new functions. But, when I try by by doing ActivateMonitoring = s, I get an error of: $s: numeric argument required. And, when I try to set it as s = ActivateMonitoring, another error comes up saying s is not a function. (Obviously; Im just trying things).
 
Old 12-24-2009, 10:10 PM   #2
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,696
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
why do you have a space between the "=" sign? to get the return value,
Code:
var=$(ActivateMonitoring)
 
Old 12-24-2009, 10:53 PM   #3
isdigit
LQ Newbie
 
Registered: Dec 2009
Posts: 6

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by ghostdog74 View Post
why do you have a space between the "=" sign? to get the return value,
Code:
var=$(ActivateMonitoring)
I do it because it is easier to read, and I normally code in C. Bash, as I am becoming to see, can be tedious. Thanks, but either way, functions only return numerical values so the return would not have worked stopping the script before var=${ActivateMonitoring}

Last edited by isdigit; 12-24-2009 at 11:04 PM.
 
Old 12-24-2009, 11:05 PM   #4
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,551
Blog Entries: 28

Rep: Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176
What does "it stops on the last bracket in Menu" mean? What are the symptoms? It would be helpful if you could also post what you see at the command prompt when you run the script.
 
Old 12-24-2009, 11:05 PM   #5
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,696
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
Quote:
Originally Posted by isdigit View Post
I do it because it is easier to read, and I normally code in C. Bash, as I am becoming to see, can be tedious.
not really. you can't have spaces when you declare variables

Quote:
Thanks, but either way, functions only return numerical values so the return would not have worked stopping the script before var=${ActivateMonitoring}
not really either. Function return numerical values , true, but you can "return" strings if you like, using echo/printf and then "catching" it from main.

here's how i would do it, using infinity loop

Code:
while true
do
cat <<EOF
1) this is option 1
2) this is option 2
3) Exit
EOF
  read -p "Enter your choice" choice
  case "$choice"in    
    1) echo "do something";;
    2) echo "do something";;
    3) exit;;
    *) echo "try again";;
  esac
done
 
Old 12-24-2009, 11:39 PM   #6
isdigit
LQ Newbie
 
Registered: Dec 2009
Posts: 6

Original Poster
Rep: Reputation: 0
Yes, I see what you mean by calling it outside in main. I did the same thing to my situation before your post, it resembled this:
Code:
#!/bin/bash

Function() {
echo "hello enter some junk: "
read junk
}

Function
echo $junk

exit 0

Last edited by isdigit; 12-24-2009 at 11:52 PM.
 
Old 12-24-2009, 11:52 PM   #7
isdigit
LQ Newbie
 
Registered: Dec 2009
Posts: 6

Original Poster
Rep: Reputation: 0
Edit: oops

Last edited by isdigit; 12-24-2009 at 11:58 PM.
 
  


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
LXer: Returning Values from Bash Functions LXer Syndicated Linux News 0 09-11-2009 11:30 PM
Bash strings evaluation matiasquestions Linux - General 6 08-20-2008 04:18 PM
Need help with bash and strings Histamine Programming 4 06-27-2007 04:18 PM
BASH: Output everything between two strings systemparadox Programming 2 12-18-2004 10:26 AM
bash and strings graziano1968 Linux - Software 2 10-01-2004 06:50 AM


All times are GMT -5. The time now is 11:57 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration