Issue while printing variables using echo - shell

I have an issue printing two variables using echo. Below is the code snippet from the script:-
tdaydatefile=$(date +'%m%d%Y')
ip=$(ip route get 8.8.8.8 | awk 'NR==1 {print $NF}')
echo "Dumping all network connections to $HOME/MyLog/netstat_$ip_$tdaydatefile.csv!"
When the script is run, it only prints below:-
Dumping all network connections to /root/MyLog/netstat_12022014.csv!
It doesn't print the ip address (ip variable) of the system. If I replace ip with tdaydatefile in echo, it prints ip and ignores tdaydatefile variable.
Any idea what could be wrong here?

Since _ is a valid variable-name character, you need to tell the shell where your variable name ends. (In your code, the shell interprets ip_ as the variable name.)
This can be done by enclosing the variable name in {...}, i.e., use ${ip} in this case:
echo "Dumping all network connections to $HOME/MyLog/netstat_${ip}_$tdaydatefile.csv!"
Note that it's not necessary with $HOME or $tdaydatefile , because / and . are not a valid variable-name chars.
When in doubt, however, use ${...} — it also helps readability.

Related

On microblaze uclinux: put IP address to variable

Yes, this is related to Putting IP Address into bash variable. Is there a better way but nothing of the ideas there work for me on the microblaze uclinux.
I wish to have my ip address of eth0 stored to a shell variable that I can write a script using it. I need alternative ideas how to do this.
ifconfig is available if that helps.
I found that in the file /etc/config/dhcp0.conf the correct ip address is stored, here's the file's content:
1 192.168.10.102
How can I remove the 1 and space without using following commands
grep
sed
cut
this also does not work: echo ${variable:2}
You can use the shell's read built-in:
read num ip </etc/config/dhcp0.conf
$num will contain the number at the beginning of the line, $ip will contain the IP.
Assign ifconfig output of eth0 to array
ifout=($(ifconfig eth0))
Strip off everything before the semicolon of the 6th element of array and assign it to the variable $ethip
ethip=${ifout[6]#*:}

Translate numbers (IP address) inside a variable (bash)

My problem is that I'm having an IP address like 10.3.1.33
This IP address is inside a variable ip=10.3.1.33
Now I want to translate the 33 inside that IP address with a "*".
The "33" can change, so that this number has to be automatically put somewhere in a variable or so.....
I have no clue how to do that. Thanks for any advice :)
In your very specific case you could use:
$ ip="10.3.1.33"
$ printf "%s\n" "${ip/33/*}"
10.3.1.*
And to replace (remove) everything after the last period:
$ ip="10.3.1.33"
$ printf "%s\n" "${ip%.*}.*"
10.3.1.33
The later is POSIX compatible while the first is available in bash (among other shells)
I can think about this:
ip=1.2.3.4
ip1=${ip%.*}.*

Remove part of $(hostname)') in bash script?

I am running a script to automatically update a field inside a database with the servers hostname
updatevar="UPDATE email_lists SET owneremail = REPLACE(owneremail, 'replacethis321', '$(hostname)');"
However, the hostname is "xyz.com"; how would I make it only be "xyz"? The script needs to remove the ".com" part. This is used on multiple servers so I can't just set it to xyz.
Instead of using $(hostname) directly, assign it to a shell variable so you can use PE expressions at expansion time:
hostname=$(hostname)
updatevar="... ${hostname%%.*} ..."
The use of %%.* trims everything after the first dot. You could instead use ${hostname%.com} if you only wanted to remove the suffix .com (but leave .org, .net, etc alone).
Alternately, instead of $(hostname), use $(hostname -s) if your operating system supports it; this is the "short" form, truncated at the first dot.
That said -- using string substitutions to form SQL expressions is bad form (and since bash doesn't typically give you a way to use bind variables in running SQL, it's usually necessary/appropriate to use a different language if you genuinely want to do the job right). Even the Wooledge bash guide explicitly calls out database interaction as a limitation.
Alternatively you can pipe to awk instead of getting the hostname in a separate variable.
$(hostname | awk -F "." '{print $1}')

Bash-script, sed with two variables doesn't work

I'm writing a script which change IP address every once in a while but for some reason I just can't get this working
sed "s/$curLine/$nextLine/" </etc/network/interfaces>/etc/network/interfaces.new, even if I change the end /g, I will always get "sed unterminated `s' command"
Here is my variables $curLine and $nextLine:
#Find current ip from /etc/network/interfaces
curLine=$(sed -n "/address/p" /etc/network/interfaces)
#Find location of current ip in ips.txt.
newLine=$(sed -n "/$curLine/=" ips.txt)
#Check and set next ip
nextIP=$(($newLine+1))
nextLine=$(sed -n "$nextIP,/address/p" ./ips.txt)
for some reason $nextLine also gives me two ips I don't know why because at the beginning ot worked just fine.
and in the end this is the line which doesn't work as it should.
"s/$curLine/$nextLine/" </etc/network/interfaces>/etc/network/interfaces.new
error message "sed unterminated `s' command". If I replace $nextline with text like "address xxx.xxx.xxx.xx it works just fine.
I have read so many topics where solution was use double quotes but I don't know why it doesn't work with me. I also have a book where's an example which is basically just same what I'm doing but again it doesn't work when I try use it.
Thanks !
The syntax for arithmetic has no extra sigil:
NUM=1
OTHERNUM=$((NUM + 1))
# ^^^^^^^^^^^^
In other words, say nextIP=$((newLine+1)).
Problem solved. I just changed
nextLine=$(sed -n "$nextIP,/address/p" ./ips.txt)
to search range of lines instead of specific line with a pattern.
nextLine=$(sed -n "$nextIP,/$nextIP" ./ips.txt)

to which subnet IP address belongs?

I have to write a script in bash , perl or python.
I got file with three columns (for manually manage connect-proxy)
SUBNET/IP socks_port socks_ip
1.2.3.* 1080 9.8.7.6
1.1.* 1080 6.8.7.6
I want to know to which subnet belongs IP address,
for example:
$ my_script 1.1.1.2
this IP belongs to 1.1.* subnet so I want back second line.
BASH: quick and dirty, use cut, then grep over the file.
PYTHON: use ip.rsplit() and then line.split()[].startswith() iterating through the file.
PERL: no idea.
Cheers!
If the file is in the format given (i.e. using *), it'll be fairly easy to use bash pattern matching to compare this to the ip address. However, as #Mark Drago pointed out, this format anything except octet-boundary subnets, so if you need to support arbitrary subnet boundaries, you need a better format.
Assuming you are using the "1.2.*" format, this should work:
#!/bin/bash
ip="$1"
found_match=false
while read subnet socks_port socks_ip; do
if [[ "$ip" == $subnet ]]; then # this'll do glob-style pattern matching against $subnet
echo "subnet=$subnet, socks_port=$socks_port, socks_ip=$socks_port"
found_match=true
break # assuming you don't want to check for multiple matches
fi
done </path/to/subnet/file
if ! $found_match; then
echo "No match found in subnet file"
fi

Resources