LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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, 10: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, 11:10 PM   #2
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
why do you have a space between the "=" sign? to get the return value,
Code:
var=$(ActivateMonitoring)
 
Old 12-24-2009, 11: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-25-2009 at 12:04 AM.
 
Old 12-25-2009, 12:05 AM   #4
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
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-25-2009, 12:05 AM   #5
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
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-25-2009, 12:39 AM   #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-25-2009 at 12:52 AM.
 
Old 12-25-2009, 12:52 AM   #7
isdigit
LQ Newbie
 
Registered: Dec 2009
Posts: 6

Original Poster
Rep: Reputation: 0
Edit: oops

Last edited by isdigit; 12-25-2009 at 12:58 AM.
 
  


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

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

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

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