LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Shell Script: "bash: Bad Substitution - Script for remove "." " (https://www.linuxquestions.org/questions/programming-9/shell-script-bash-bad-substitution-script-for-remove-4175594999/)

thiagofw 12-07-2016 03:06 PM

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.

Habitual 12-07-2016 03:18 PM

I think "ip" is a binary on some systems.

Code:

whereis ip
may say what/where

thiagofw 12-07-2016 03:45 PM

Quote:

Originally Posted by Habitual (Post 5639256)
I think "ip" is a binary on some systems.

Code:

whereis ip
may say what/where

I've changed and still continues with the problem
:(

michaelk 12-07-2016 03:51 PM

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//./}


thiagofw 12-07-2016 04:05 PM

Quote:

Originally Posted by michaelk (Post 5639270)
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 ! :)

thiagofw 12-07-2016 04:11 PM

Now i use if and compare 19216815 is gt 192168254254 ...
if 192.168.1.5 > 192.168.254.254 then ; [...error]else[...ok]

michaelk 12-07-2016 04:12 PM

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.

thiagofw 12-07-2016 04:40 PM

Quote:

Originally Posted by michaelk (Post 5639278)
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


michaelk 12-07-2016 05:37 PM

I prefer using [[ ]] for conditionals but is the following real code or a comment?
Code:

Test if ip > 192.168.254.254

thiagofw 12-07-2016 05:54 PM

Quote:

Originally Posted by michaelk (Post 5639307)
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.. :)

c0wb0y 12-07-2016 06:26 PM

Are you able to post the new changes? The whole revised script is even better.

thiagofw 12-07-2016 07:25 PM

Quote:

Originally Posted by c0wb0y (Post 5639316)
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 :)

c0wb0y 12-07-2016 07:45 PM

Now I'm lost.... :)

thiagofw 12-07-2016 08:13 PM

Quote:

Originally Posted by c0wb0y (Post 5639336)
Now I'm lost.... :)

:redface:
Owww. My englis is .... hehehe kkk
I'm changing that, I understood that you had overhauled this part of the script.

thiagofw 12-09-2016 10:04 PM

My problem is solved using this post.
http://www.linuxquestions.org/questi...9/#post5639270
Thank you brothers.
:)


All times are GMT -5. The time now is 06:43 PM.