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 12-07-2016, 03:06 PM   #1
thiagofw
Member
 
Registered: Jul 2016
Location: Minas Gerais, Brazil
Distribution: Slackware 14.2
Posts: 42

Rep: Reputation: Disabled
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 03:13 PM. Reason: insert information
 
Old 12-07-2016, 03:18 PM   #2
Habitual
LQ Veteran
 
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
Posts: 9,374
Blog Entries: 37

Rep: Reputation: Disabled
I think "ip" is a binary on some systems.

Code:
whereis ip
may say what/where
 
Old 12-07-2016, 03:45 PM   #3
thiagofw
Member
 
Registered: Jul 2016
Location: Minas Gerais, Brazil
Distribution: Slackware 14.2
Posts: 42

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Habitual View Post
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
 
Old 12-07-2016, 03:51 PM   #4
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,669

Rep: Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892
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//./}
 
Old 12-07-2016, 04:05 PM   #5
thiagofw
Member
 
Registered: Jul 2016
Location: Minas Gerais, Brazil
Distribution: Slackware 14.2
Posts: 42

Original Poster
Rep: Reputation: Disabled
Thumbs up

Quote:
Originally Posted by michaelk View Post
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 !
 
Old 12-07-2016, 04:11 PM   #6
thiagofw
Member
 
Registered: Jul 2016
Location: Minas Gerais, Brazil
Distribution: Slackware 14.2
Posts: 42

Original Poster
Rep: Reputation: Disabled
Now i use if and compare 19216815 is gt 192168254254 ...
if 192.168.1.5 > 192.168.254.254 then ; [...error]else[...ok]
 
Old 12-07-2016, 04:12 PM   #7
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,669

Rep: Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892
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.
 
Old 12-07-2016, 04:40 PM   #8
thiagofw
Member
 
Registered: Jul 2016
Location: Minas Gerais, Brazil
Distribution: Slackware 14.2
Posts: 42

Original Poster
Rep: Reputation: Disabled
Talking

Quote:
Originally Posted by michaelk View Post
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
 
Old 12-07-2016, 05:37 PM   #9
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,669

Rep: Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892
I prefer using [[ ]] for conditionals but is the following real code or a comment?
Code:
Test if ip > 192.168.254.254
 
Old 12-07-2016, 05:54 PM   #10
thiagofw
Member
 
Registered: Jul 2016
Location: Minas Gerais, Brazil
Distribution: Slackware 14.2
Posts: 42

Original Poster
Rep: Reputation: Disabled
Post

Quote:
Originally Posted by michaelk View Post
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..
 
Old 12-07-2016, 06:26 PM   #11
c0wb0y
Member
 
Registered: Jan 2012
Location: Inside the oven
Distribution: Windows
Posts: 417

Rep: Reputation: 74
Are you able to post the new changes? The whole revised script is even better.
 
Old 12-07-2016, 07:25 PM   #12
thiagofw
Member
 
Registered: Jul 2016
Location: Minas Gerais, Brazil
Distribution: Slackware 14.2
Posts: 42

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by c0wb0y View Post
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
 
Old 12-07-2016, 07:45 PM   #13
c0wb0y
Member
 
Registered: Jan 2012
Location: Inside the oven
Distribution: Windows
Posts: 417

Rep: Reputation: 74
Now I'm lost....
 
Old 12-07-2016, 08:13 PM   #14
thiagofw
Member
 
Registered: Jul 2016
Location: Minas Gerais, Brazil
Distribution: Slackware 14.2
Posts: 42

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by c0wb0y View Post
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.
 
Old 12-09-2016, 10:04 PM   #15
thiagofw
Member
 
Registered: Jul 2016
Location: Minas Gerais, Brazil
Distribution: Slackware 14.2
Posts: 42

Original Poster
Rep: Reputation: Disabled
My problem is solved using this post.
http://www.linuxquestions.org/questi...9/#post5639270
Thank you brothers.
 
  


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
[SOLVED] Bash - Capture "Child" script output with "Parent" without using files or coproc keyword Jason_25 Programming 2 02-14-2016 07:51 PM
bash script: using "select" to show multi-word options? (like "option 1"/"o zidane_tribal Programming 7 12-19-2015 01:03 AM
bash shell script error!!! go to "usernameftp=$username"ftp" slufoot80 Linux - Server 6 08-27-2012 03:51 PM
[SOLVED] Errors executing shell script: "command not found" and "no such file or directory" eko000 Linux - Newbie 1 01-14-2011 07:54 AM
Shell script: I have string "abc____def____ghi", how to make "abc def ghi" vouser Programming 8 03-09-2010 10:01 PM

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

All times are GMT -5. The time now is 03:59 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