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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
12-07-2016, 04:06 PM
|
#1
|
Member
Registered: Jul 2016
Location: Minas Gerais, Brazil
Distribution: Slackware 14.2
Posts: 42
Rep:
|
Shell Script: "bash: Bad Substitution - Script for remove "." "
I am writing a program (script) and certain snippet of code requires that you remove the "." Of the network mask, ips, dns and others present in the insertion of data by the user.
The problem is that on a certain operating system (slackware 14.2) it runs perfectly, but the same script when released in kali 2016 (debian base ) of the error when executing the script.
Code:
#!/bin/bash
#
echo "Enter the IP Address"
read ip
echo "Enter the subnet mask"
read mask
newmask=$(echo ${mask//./})
newip=$(echo ${ip//./})
echo "the value of newmask is :" $newmask
echo "the value of newip is :" $newip
echo "the value of ip is :" $ip
echo "the value of mask is :" $mask
In Slackware 14.2 the result of running this script is::
Code:
bash-4.3# sh linuxquestions-my-question.sh
Enter the IP Address
192.168.1.1
Enter the subnet mask
255.255.255.0
the value of newmask is : 2552552550
the value of newip is : 19216811
the value of ip is : 192.168.1.1
the value of mask is : 255.255.255.0
bash-4.3#
And in Kali Linux the result is::
Code:
root@srv-001:/logs# sh linuxquestions-my-script.sh
Enter the IP Address
192.168.2.1
Enter the subnet mask
255.255.255.0
linuxquestions-my-script.sh: 13: linuxquestions-my-script.sh: Bad substitution
linuxquestions-my-script.sh: 16: linuxquestions-my-script.sh: Bad substitution
the value of newmask is :
the value of newip is :
the value of ip is : 192.168.2.1
the value of mask is : 255.255.255.0
Until I found another way to do this, but now that I saw that it may be a mistake I really wanted to keep this script ... hehehe
Thank you.
Last edited by thiagofw; 12-07-2016 at 04:13 PM.
Reason: insert information
|
|
|
12-07-2016, 04:18 PM
|
#2
|
LQ Veteran
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
Posts: 9,374
Rep:
|
I think "ip" is a binary on some systems.
may say what/where
|
|
|
12-07-2016, 04:45 PM
|
#3
|
Member
Registered: Jul 2016
Location: Minas Gerais, Brazil
Distribution: Slackware 14.2
Posts: 42
Original Poster
Rep:
|
Quote:
Originally Posted by Habitual
I think "ip" is a binary on some systems.
may say what/where
|
I've changed and still continues with the problem
|
|
|
12-07-2016, 04:51 PM
|
#4
|
Moderator
Registered: Aug 2002
Posts: 26,364
|
It depends on your default shell. What is the output of the command
ls -l /bin/sh
What happens when your run it via bash instead of sh
bash linuxquestions-my-script.sh
FYI you do not need the echo...
Code:
newmask=${mask//./}
newip=${ip//./}
|
|
|
12-07-2016, 05:05 PM
|
#5
|
Member
Registered: Jul 2016
Location: Minas Gerais, Brazil
Distribution: Slackware 14.2
Posts: 42
Original Poster
Rep:
|
Quote:
Originally Posted by michaelk
It depends on your default shell. What is the output of the command
ls -l /bin/sh
What happens when your run it via bash instead of sh
bash linuxquestions-my-script.sh
FYI you do not need the echo...
Code:
newmask=${mask//./}
newip=${ip//./}
|
Hello.
the output command ls -l /bin/sh is :
Code:
root@srv-001:/logs# ls -l /bin/sh
lrwxrwxrwx 1 root root 4 Ago 1 2016 /bin/sh -> dash
Code:
root@srv-001:/logs# bash linuxquestions-my-script.sh
Enter the Ip Addres:
192.168.1.5
Enter the subnet mask:
255.255.255.0
the value of newmask is : 19216815
the value of newip is : 2552552550
the value of ip is : 192.168.1.5
the value of mask is : 255.255.255.0
root@srv-001:/logs#
I just changed the flame to call the " bash" from "sh" to "bash" and it worked perfectly.
There is a simpler way to remove the "." Of network masks?
I am very grateful to the clarifications, very much.
Vlw ....
Ahhh , its OK !
|
|
|
12-07-2016, 05:11 PM
|
#6
|
Member
Registered: Jul 2016
Location: Minas Gerais, Brazil
Distribution: Slackware 14.2
Posts: 42
Original Poster
Rep:
|
Now i use if and compare 19216815 is gt 192168254254 ...
if 192.168.1.5 > 192.168.254.254 then ; [...error]else[...ok]
|
|
|
12-07-2016, 05:12 PM
|
#7
|
Moderator
Registered: Aug 2002
Posts: 26,364
|
In addition using the shebang (#!/bin/bash) in the first line of the script allows you to run it as an executable. Just change permissions.
chmod 755 linuxquestions-my-script.sh
You can run it from the same directory as
./linuxquestions-my-script.sh
or using the full path.
Post the actual if-then code.
|
|
|
12-07-2016, 05:40 PM
|
#8
|
Member
Registered: Jul 2016
Location: Minas Gerais, Brazil
Distribution: Slackware 14.2
Posts: 42
Original Poster
Rep:
|
Quote:
Originally Posted by michaelk
In addition using the shebang (#!/bin/bash) in the first line of the script allows you to run it as an executable. Just change permissions.
chmod 755 linuxquestions-my-script.sh
You can run it from the same directory as
./linuxquestions-my-script.sh
or using the full path.
Post the actual if-then code.
|
I'm sorry, I had to translate the outputs and others PT.BR for EN.US
The variables used are the program variables.
The questions are the real ones.
I have declared values for variables that will be the limits of both ip's and netmask.
After the user enters values of ip and mask I do the following tests:
Code:
#!/bin/sh
# This script change de interfaces configuration and add ips using you
# choice.
# This program is totally free, this program is
## BLOCO 1 - TESTES E VARIAVEIS #
##+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++(1a inicio)
## começa variaveis
#. /home/thiago/gatewaylinux.com/info/config.pt-br
prog_name=slack.firewall
#prog_name_default=auto.net.inf
user=`whoami`
data=`date`
maskmax="255255255252"
maskmin="255000"
ipmax="192168254254"
ipmin="172001"
progconf=$(/etc/network/interfaces2)
#Fim de variaveis
#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++(1a fim )
######Remendo para pegar as interfaces :@ <msn>
inet1=`ifconfig|grep eth0 |cut -c 1-4`
[...]
#++++++++++++++++++++++++++++++++++++++++++++++++++++(1b inicio)
if [ $user != "root" ]; then
echo "error...root requerid for run this script - not run the program? chmod +x $0";
else
echo "download script..."
fi
# FIM DO BLOCO 1 #
##=+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++(1b fim)
#####inicio pega valores
####Meu Deus, quanto comentario, bom que pelo menos documenta né?
echo "Digite um IP a ser adicionado. . ."
read ip
echo "Digite uma Mascara para o IP a ser adiconado. . ."
read mask
###JJá ouviu Otherside dos Peppers? Muito Show
##fim do pega valores
if test $newmask -lt $maskmin ; then
echo "Error please use val ... 255.255.255.252 and 255.0.0.0 "
else
echo "Nova Mascara Aceita"
fi
#Testa agora o maior valor #
if test $newmask -lt $maskmax ; then
echo "OK... Mask is add"
else
echo "Error please use val ... 255.255.255.252 and 255.0.0.0 "
fi
Test if ip > 192.168.254.254
if test $newip -lt $ipmin ; then
echo "Error, use val -> 172.0.0.0 and 192.168.254.254"
else
echo "OK"
fi
#Testa maior valor de IPS #
if test $newip -lt $ipmax ; then
echo "OK"
else
echo "Error, use val -> 172.0.0.0 and 192.168.254.254"
fi
[. . .]
end of script
|
|
|
12-07-2016, 06:37 PM
|
#9
|
Moderator
Registered: Aug 2002
Posts: 26,364
|
I prefer using [[ ]] for conditionals but is the following real code or a comment?
Code:
Test if ip > 192.168.254.254
|
|
|
12-07-2016, 06:54 PM
|
#10
|
Member
Registered: Jul 2016
Location: Minas Gerais, Brazil
Distribution: Slackware 14.2
Posts: 42
Original Poster
Rep:
|
Quote:
Originally Posted by michaelk
I prefer using [[ ]] for conditionals but is the following real code or a comment?
Code:
Test if ip > 192.168.254.254
|
Ohh, its a comment.
my error.
i use [[ ]] but i find errors, my erros..
|
|
|
12-07-2016, 07:26 PM
|
#11
|
Member
Registered: Jan 2012
Location: Inside the oven
Distribution: Windows
Posts: 421
Rep:
|
Are you able to post the new changes? The whole revised script is even better.
|
|
|
12-07-2016, 08:25 PM
|
#12
|
Member
Registered: Jul 2016
Location: Minas Gerais, Brazil
Distribution: Slackware 14.2
Posts: 42
Original Poster
Rep:
|
Quote:
Originally Posted by c0wb0y
Are you able to post the new changes? The whole revised script is even better.
|
Of course!
But it still does not 'complete' the script completely.
My intention is that over the course of my days, when I have little work, extra time and / or tired of studying Python, I can improve it and, along with people like you, provide gateway configuration scripts, firewall, samba, An easy and simple way, without websites, without blogs. Something like this -> http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO.html#toc1
Without advertisements, without much mess, something clean .. What do you think of the idea? I know there is a lot of stuff, but it's never too much to have another exit, or have a second option and even study shell script.
As for the proposal, I clearly agree, it would never go against a friend of free software.
The first script is deb1.txt in http://gatewaylinux.info/gatewaylinux.info/deb1.txt
|
|
|
12-07-2016, 08:45 PM
|
#13
|
Member
Registered: Jan 2012
Location: Inside the oven
Distribution: Windows
Posts: 421
Rep:
|
Now I'm lost....
|
|
|
12-07-2016, 09:13 PM
|
#14
|
Member
Registered: Jul 2016
Location: Minas Gerais, Brazil
Distribution: Slackware 14.2
Posts: 42
Original Poster
Rep:
|
Quote:
Originally Posted by c0wb0y
Now I'm lost....
|
Owww. My englis is .... hehehe kkk
I'm changing that, I understood that you had overhauled this part of the script.
|
|
|
All times are GMT -5. The time now is 10:53 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|