I need some Specs and Data from Number of Latops in a ini-file.
When I try to get the IP-Adress with ip addr list <interface> nothing happens in the script. Not even a Error.
I try to quote stuff in my code. Nothing change.
for w2 in /sys/class/net/wl*
do
w2i=$(basename $w2)
echo $w2i
addr=$(ip -o -4 addr list $w2i | awk '{print $4}' | cut -d/ -f1)
echo $addr
echo -e "$w2i=$addr"
done
I Think maybe it is because of the Varibales but this fails also (interface is set 'manually'):
for w2 in /sys/class/net/wl*
do
w2i=$(basename $w2)
echo $w2i
addr=$(ip -o -4 addr list wlp3s0 | awk '{print $4}' | cut -d/ -f1)
echo $addr
echo -e "$w2i=$addr"
done
When i run the script i get wlp3s0 and i must close the script with ctl-c. When i run ip -o -4 addr list wlp3s0 | awk '{print $4}' | cut -d/ -f1 it gives me my ip like i expected.
EDIT
I just need to wirte the full-path of the ip-command. Can somebody explain why i have to do this? And especially it is not necessary for cut or awk that also based in /usr/bin/?
for w2 in /sys/class/net/wl*
do
w2i=$(basename $w2)
echo $w2i
addr=$(/usr/bin/ip -o -4 addr list "$w2i" | awk '{print $4}' | cut -d/ -f1)
echo $addr
echo -e "$w2i=$addr"
done
}
Related
I have a linux script for selecting the node.
For example:
4
40*r13n15:40*r10n61:40*r11n18:40*r09n15
The correct result should be:
r13n15
r10n61
r11n18
r09n15
My linux script content is like:
hostNum=`bjobs -X -o "nexec_host" $1 | grep -v NEXEC`
hostSer=`bjobs -X -o "exec_host" $1 | grep -v EXEC`
echo $hostNum
echo $hostSer
for i in `seq 1 $hostNum`
do
echo $hostSer | awk -F ':' '{print '$i'}' | awk -F '*' '{print $2}'
done
But unlucky, I got nothing about node information.
I have tried:
echo $hostSer | awk -F ':' '{print "'$i'"}' | awk -F '*' '{print $2}'
and
echo $hostSer | awk -F ':' '{print '"$i"'}' | awk -F '*' '{print $2}'
But there are wrong. Who can give me a help?
One more awk:
$ echo "$variable" | awk 'NR%2==0' RS='[*:\n]'
r13n15
r10n61
r11n18
r09n15
By setting the record separtor(RS) to *:\n , the string is broken into individual tokens, after which you can just print every 2nd line(NR%2==0).
You can use multiple separators in awk. Please try below:
h='40*r13n15:40*r10n61:40*r11n18:40*r09n15'
echo "$h"| awk -F '[:*]' '{ for (i=2;i<=NF;i+=2) print $i }'
**edited to make it generic based on the comment from RavinderSingh13.
I have a task which asks to write a script which displays all partitions formatted with a specific file system, given as parameter.
I have written the script but when i run it it displays '0'. What am i doing wrong?
This is my code:
#!/bin/bash
n=sudo parted -l | tail -n +8 | awk '{print $5}' | wc | awk '{print $2}'
m=sudo parted -l | tail -n +8 | awk '{print $5}'
q=sudo parted -l | tail -n +8
for i in $n; do
if [ "[ $m | sed -n ip ]" = "$1" ]; then
echo "$q | sed -n ip"
fi
done
Different approach from yours, but does it do what you need?
lsblk -f | awk '$0 ~ fs {print $NF}' fs=ext2
Hoping for some input on this, as I'm struggling. I have a csv that contains an IP mask in which I want to get the network IP and broadcast IP.
So for instance I want the input field 1.0.0.0/24 to output 2 new field that contain the vaules: 1.0.0.0 in one and 1.0.0.255 in the other.
I have code to do this.
For broadcast:
for i in $(cat geoip.csv);do bcaddr=$(ipcalc -n -b $i);echo
${bcaddr#BROADCAST=};done
And for network:
for i in $(cat geoip.csv);do bcaddr=$(ipcalc -n -4 $i);echo
${bcaddr#BROADCAST=};done
Where do I go from here? How do I go about appending these 2 new fields to a new output file?
Thanks in advance!
see if this works on RHEL 5.x... do you want the output in csv with comma separator?
for i in $(cat csip.csv); do echo "$i $(ipcalc -n -b $i | grep -E "Address|Broadcast" | awk {'print ","$2'} | tr '\n' ' ')" ; done > new.csv
For your version of ipcalc, please try this:
for i in $(cat csip.csv); do echo "$i $(ipcalc -n -b $i | grep -E "NETWORK|BROADCAST" | awk -F= {'print ","$2'} | tr '\n' ' ')" ; done > new.csv
Why not using awk?
If you are using Linux (Not Unix):
for i in $(cat geoip.csv);do bcaddr=$(ipcalc -n -b $i); echo $bcaddr | egrep -o '([0-9]+\.){3}[0-9]+' ; done > ip.txt
Output:
1.0.0.255
1.0.0.0
If you want the output in a single line:
for i in $(cat geoip.csv);do bcaddr=$(ipcalc -n -b $i); echo $bcaddr | egrep -o '([0-9]+\.){3}[0-9]+' ORS=' ' ; done}' > ip.txt
Output:
1.0.0.255 1.0.0.0
I am making a script that will let you choose between which interface you want to use.
I need a way to get the interfaces and store each of them in a variable.
Here is my code, but it only gets the interfaces:
Interfaces=$(ifconfig | awk '{print $1}' | grep ':' | tr -d ':')
You need to only check the lines that contain the interface name, not the lines with details. In ifconfig, detail lines start with a space; in ip, interface lines start with a number.
In bash, you can use select to create a simple menu:
#! /bin/bash
select interface in $(ip link show | grep '^[0-9]' | cut -f2 -d:) ; do
if [[ $interface ]] ; then
echo You selected $interface
break
fi
done
or
select interface in $(ifconfig -a | grep -v '^ ' | cut -f1 -d' ') ; do
if [[ $interface ]] ; then
echo You selected $interface
break
fi
done
I have a tomcat server I am trying to get a list of info on for a project. I need to get the results from /etc/default/tomcat file. However some of my servers are tomcat6 and some are tomcat7 so hardcoding the filename is not going to work.
How would I dynamically insert the filename in this batch script.
#!/bin/bash
echo Server Name: `hostname`
echo CPU: `top -b -n1 | grep "Cpu(s)" | awk '{print $2 + $4}'`
FREE_DATA=`free -m | grep Mem`
CURRENT=`echo $FREE_DATA | cut -f3 -d' '`
TOTAL=`echo $FREE_DATA | cut -f2 -d' '`
echo Internal IP : `ifconfig eth0 2>/dev/null|awk '/inet addr:/ {print $2}'|sed 's/addr://'`
echo OS Memory: `cat /proc/meminfo | grep MemTotal | awk '{ print $2 }'`
echo Operating System: `uname -mrs`
***echo Tomcat Memory: `cat /etc/default/tomcat6 | grep Xmx | awk '{ print $5}'`***
Your last command can be simplified to single awk like this:
awk '/Xmx/{print "Tomcat Memory:", $5}' "$tomcatFile"
Pass "$tomcatFile" whatever tomcat filename from ver6 or ver7.
You can get output from both tomcat files in same command using:
awk '/Xmx/{print "Tomcat Memory:", $5}' /etc/default/tomcat[67]