LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-14-2010, 09:10 AM   #1
tnght2001
LQ Newbie
 
Registered: Jun 2010
Posts: 8

Rep: Reputation: 0
in value


#!/bin/bash
function e {
echo -n "What eth between 1 to 10: "
read n
r $n
}
e

function r {
ifconfig eth$n
}
r

i want to user to type a number than it will display eth1 or eth2, but got error and error say:
./d.sh: line 5: r: command not found
eth2: error fetching interface information: Device not found
can help me to fix it pls?
 
Old 07-14-2010, 09:20 AM   #2
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
Well....first, what is the "r" command supposed to do? OK--I see that it is the function you defined----I wonder if you have to define the function before using it?

Does your system have eth2? (Mine does not.)
 
Old 07-14-2010, 09:21 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,627

Rep: Reputation: 2943Reputation: 2943Reputation: 2943Reputation: 2943Reputation: 2943Reputation: 2943Reputation: 2943Reputation: 2943Reputation: 2943Reputation: 2943Reputation: 2943
Well there are a number of issues, the first being, please use [ code ] [/ code ] tags (without the spaces) so we can read your code.

As for the code:

Quote:
./d.sh: line 5: r: command not found
This error is because shell scripting is a top down language, meaning that up until line 5 the function r has yet to be defined

Second problem, if you are going to use global variable, ie the variable 'n', why bother having separate functions?
Choices are to amalgamate the entire script and define n only once or use the reserved word local to create them so they only exist within each function.

Part two of the second choice is to the call the functions correctly, ie with the passed in values not the global ones. This mean that your r function
would become something like:
Code:
function r{
    ifconfig eth$1
}
[quote]eth2: error fetching interface information: Device not found[/code]
This error is not related to your code but rather that you have no such device called eth2
 
1 members found this post helpful.
Old 07-15-2010, 08:05 PM   #4
tnght2001
LQ Newbie
 
Registered: Jun 2010
Posts: 8

Original Poster
Rep: Reputation: 0
Thanks for the help....
 
Old 07-15-2010, 08:07 PM   #5
tnght2001
LQ Newbie
 
Registered: Jun 2010
Posts: 8

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by pixellany View Post
Well....first, what is the "r" command supposed to do? OK--I see that it is the function you defined----I wonder if you have to define the function before using it?

Does your system have eth2? (Mine does not.)
My assignment en-quire us to have eth1,2,3...
 
Old 07-15-2010, 09:24 PM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,627

Rep: Reputation: 2943Reputation: 2943Reputation: 2943Reputation: 2943Reputation: 2943Reputation: 2943Reputation: 2943Reputation: 2943Reputation: 2943Reputation: 2943Reputation: 2943
Please mark as SOLVED once you have a solution.
 
Old 07-18-2010, 04:35 AM   #7
tnght2001
LQ Newbie
 
Registered: Jun 2010
Posts: 8

Original Poster
Rep: Reputation: 0
i got other question to ask
#!/bin/bash
echo "Enter new hostname: "
read i
echo "Enter new namesever: "
read y

sysctl kernel.hostname=$i

if [ y != regex[:digit:] ] ; then
sed = /etc/resolv.conf | sed [ -e /etc/resolv.conf]

else
echo "error"

fi

i am try to ask the user to change the namesever but i fail to do so so pls help =)
 
Old 07-18-2010, 05:45 AM   #8
tnght2001
LQ Newbie
 
Registered: Jun 2010
Posts: 8

Original Poster
Rep: Reputation: 0
and other one, just help where i go wrong always got error say
Usage: inet_route [-vF] del {-host|-net} Target[/prefix] [gw Gw] [metric M] [[dev] If]
inet_route [-vF] add {-host|-net} Target[/prefix] [gw Gw] [metric M]
[netmask N] [mss Mss] [window W] [irtt I]
[mod] [dyn] [reinstate] [[dev] If]
inet_route [-vF] add {-host|-net} Target[/prefix] [metric M] reject
inet_route [-FC] flush NOT supported
./i.sh: line 162: netMask: command not found
./i.sh: line 177: syntax error near unexpected token `}'
./i.sh: line 177: `}'


#!/bin/bash


function q {
echo "Do you want to add/change "
echo "1.ip address"
echo "2.gateway"
echo "3.netmask"
echo "4.eth"
echo -n "Select 1 -4: "
read n

if [ $n -eq 1 ] ; then
clear
ie

elif [ $n -eq 2 ] ; then
clear
gateWay

elif [ $n -eq 3 ] ; then
clear
netMask

elif [ $n -eq 4 ] ; then
clear
eth

else
clear
q

fi
}
q

function ie {
echo "type address"
read t

oldIFS=$IFS
IFS=.

set -- $t


if [ "$#" -ne "4" ] ; then
clear
echo $t "Wrong ip address"
echo "Must type between 1 - 245"
echo "10.2.2.3"
ie
fi

for oct in $1 $2 $3 $4; do
echo $oct | egrep "^[0-9]+$" >/dev/null 2>&1
if [ "$?" -ne "0" ] ; then
clear
echo "$oct: Not numeric"
ie

else
if [ "$oct" -lt "0" -o "$oct" -gt "255" ] ; then
clear
echo "$oct: Out of range"
ie

fi
fi
done

#if is ok
echo "You have success add"
echo "route add $t"
route add -net $t

}
ie

function gateWay {
echo
echo "type gateway"
read i

oldIFS=$IFS
IFS=.

set -- $i

if [ "$#" -ne "4" ] ; then
clear
echo $i "Wrong ip address"
echo "Must type between 1 - 245"
echo "10.2.2.3"
p
fi

for oct in $1 $2 $3 $4; do
echo $oct | egrep "^[0-9]+$" >/dev/null 2>&1
if [ "$?" -ne "0" ] ; then
clear
echo "$oct: Not numeric"
gateWay

else
if [ "$oct" -lt "0" -o "$oct" -gt "255" ] ; then
clear
echo "$oct: Out of range"
gateWay

fi
fi
done

#if is ok
echo "You have success add"
echo "gateway $i
route add -net gw $i
}
gateWay

function netMask {
echo
echo "type netmask"
read e

oldIFS=$IFS
IFS=.

set -- $e

if [ "$#" -ne "4" ] ; then
clear
echo $e "Wrong ip address"
echo "Must type between 1 - 245"
echo "10.2.2.3"
p
fi

for oct in $1 $2 $3 $4; do
echo $oct | egrep "^[255]+$" >/dev/null 2>&1
if [ "$?" -eq "255" ] ; then
clear
echo "$oct: Not numeric"
netMask

else
if [ "$oct" -lt "0" -o "$oct" -gt "255" ] ; then
clear
echo "$oct: Out of range"
netMask

fi
fi
done

#if is ok
echo "You have success add"
echo "netmask $e
route add -net netmask $e
}
netMask

function eth {
echo "type eth"
read p

if [ $p -lt 3 ] ; then
clear
echo "You have add eth$p "
route add -net dev eth0

else
clear
echo "Error"
eth
}
eth

function p {
echo
echo "Do you want to"
echo "1.Type address & gateway"
echo "2.Go back"
echo -n "Select 1 - 2 "
read n

if [ $n -eq 1 ] ; then
clear
q

elif [ $n -eq 2 ] ; then
clear
./Ts.sh

else
clear
p

fi
}
p
 
Old 07-18-2010, 05:48 AM   #9
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,627

Rep: Reputation: 2943Reputation: 2943Reputation: 2943Reputation: 2943Reputation: 2943Reputation: 2943Reputation: 2943Reputation: 2943Reputation: 2943Reputation: 2943Reputation: 2943
So it appears you missed the part where I asked you to use the code tags to make your code more readable.

Secondly, are you just throwing command at us so we will fix them for you?

I ask as you have identified that you are being taught, ie this is an assignment, but you are making mistakes that defy any teaching that I can see??

So instead of telling you how, I am going to ask the questions:

Please identify, one at a time, what you think the following lines of code are doing (meant to do according to your mind):

Code:
sysctl kernel.hostname=$i
Code:
if [ y != regex[:digit:] ] ; then
Code:
sed = /etc/resolv.conf | sed [ -e /etc/resolv.conf]
Until we know what you want to do, which cannot be discerned by what we see here, we will not be able to assist.
 
Old 07-18-2010, 05:50 AM   #10
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,627

Rep: Reputation: 2943Reputation: 2943Reputation: 2943Reputation: 2943Reputation: 2943Reputation: 2943Reputation: 2943Reputation: 2943Reputation: 2943Reputation: 2943Reputation: 2943
Quote:
Usage: inet_route [-vF] del {-host|-net} Target[/prefix] [gw Gw] [metric M] [[dev] If]
inet_route [-vF] add {-host|-net} Target[/prefix] [gw Gw] [metric M]
[netmask N] [mss Mss] [window W] [irtt I]
[mod] [dyn] [reinstate] [[dev] If]
inet_route [-vF] add {-host|-net} Target[/prefix] [metric M] reject
inet_route [-FC] flush NOT supported
./i.sh: line 162: netMask: command not found
./i.sh: line 177: syntax error near unexpected token `}'
./i.sh: line 177: `}'
Every message here can be googled and are stating the exact problems with your code.

Try and do some work.
 
  


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



LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:32 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration