Parsing in bash - extract content from brackets - bash

The input to my bash script can be of the form [fec1::1]:80 or []:80. The second input implies that there's no IP address given. My bash script is to split the input into IP and port. With the said second input, the script should 'understand' that no IP was given.
The following logic seems to solve my problem, on the bash prompt:
$ ip=[]:78
$ echo $ip
[]:78
$ temp=(`echo $ip | awk -F'[][]' '{print $2}'`)
$ echo $temp
$
When I try to do the same thing from within a script, the result is different:
local_endpoint="$1"
printf 'local_endpoint: %s\n' "$local_endpoint"
IN="$local_endpoint"
local_ip=$(echo "$IN" | awk -F'[][]' '{print $2}')
if [ -z "$local_ip" ] ; then
local_ip=$(echo "$IN" | awk -F':' '{print $1}')
local_port=$(echo "$IN" | awk -F':' '{print $2}')
else
local_port=$(echo "$IN" | awk -F'[][]' '{print $3}' | awk -F':' '{print $2}')
fi
printf 'IP: %s\n' $local_ip
printf 'port: %d\n' $local_port
if [ -z "$local_port" -a -z "$local_ip" ] ; then
printf 'No port and IP was given\n'
elif [ -z "$local_ip" ] ; then
printf 'No IP was given\n'
elif [ -z "$local_port" ] ; then
printf 'No port was given\n'
fi
exit 2
Output:
# ./temp.sh []:829
local_endpoint: []:829
IP: []
port: 829
Any idea on what's happening? Also, why do I see the extra comma (,) at the end of the output?

Your script is missing quoting at many places and there are stray commas too in printf. This script should work:
local_endpoint="$1"
printf 'local_endpoint: %s\n' "$local_endpoint"
IN="$local_endpoint"
if [[ "$IN" == "["* ]] ; then
local_ip=$(echo "$IN" | awk -F'[][]' '{print $2}')
local_port=$(echo "$IN" | awk -F'[][]' '{print $3}' | awk -F':' '{print $2}')
else
local_ip=$(echo "$IN" | awk -F':' '{print $1}')
local_port=$(echo "$IN" | awk -F':' '{print $2}')
fi
printf 'IP: <%s>\n' "$local_ip"
printf 'port: <%d>\n' "$local_port"
if [ -z "$local_port" -a -z "$local_ip" ] ; then
printf 'No port and IP was given\n'
elif [ -z "$local_ip" ] ; then
printf 'No IP was given\n'
elif [ -z "$local_port" ] ; then
printf 'No port was given\n'
fi
exit 2

Process substitution is:
var=$(command ...)
not
var=(command ...)

Related

Bash awk: parsing variable string into another variable? [duplicate]

This question already has answers here:
Linux bash: Multiple variable assignment
(6 answers)
Closed 4 years ago.
I would like to extract all the values contained in $line and put them in variables var1, var2,... varn. I did use this previously to extract the vars from file (in.txt)
var1=$(awk '{print $1}' < in.txt)
var2=$(awk '{print $2}' < in.txt)
....
varn=$(awk '{print $n}' < in.txt)
How should I change my awk call so as to use $line instead of in.txt?
I tried these for example
echo $line | var2=$(awk '{print $2}')
or
var2=$(echo $line | awk '{print $2}')
but without success...
========== DETAIL==============
----- calling file:
.....
name=Matrix
line=$(sed -n '/^\[T\]/ {n;p}' in.txt)
echo 'line: ' $line
L1=$(./procline_matrix_vars.sh $line 30 $name)
echo 'L1: ' $L1
------- rocline_matrix_vars.sh:
#!/bin/bash
line=$1
choice=$2
var1=$(echo $line | awk '{print $1}')
var2=$(echo $line | awk '{print $2}')
var3=$(echo $line | awk '{print $3}')
var4=$(echo $line | awk '{print $4}')
if [ $choice == 30 ]; then
L1=$(printf '\n\n\n%s = [ %s %s %s %s \n' "$3" "$var1" "$var2" "$var3" "$var4")
fi
echo "${L1%.}"
a possible way:
line="aaa bbb ccc"
var=( $line )
echo "${var[1]}"
echo "my array has ${#var[#]} elements"
output
bbb
my array has 3 elements
maybe shortcut
var=( $( awk '{print $1, $2, $10}' file ) )

shell script : comma in the beginning instead of end

This is a part of my shell script.
for line in `cat $1`
do
startNum=`echo $line | awk -F "," '{print $1}'`
endNum=`echo $line | awk -F "," '{print $2}'`
operator=`echo $line | awk -F "," '{print $3}'`
termPrefix=`echo $line | awk -F "," '{print $4}'`
if [[ "$endNum" == 81* ]] || [[ "$endNum" == 33* ]] || [[ "$endNum" == 55* ]]
then
areaCode="${endNum:0:2}"
series="${endNum:2:4}"
startCLI="${startNum:6:4}"
endCLI="${endNum:6:4}"
else
areaCode="${endNum:0:3}"
series="${endNum:3:3}"
startCLI="${startNum:6:4}"
endCLI="${endNum:6:4}"
fi
echo "Add,${areaCode},${series},${startCLI},${endCLI},${termPrefix},"
#>> ${File}
done
input is csv contains below many rows :
5557017101,5557017101,102,1694
5515585614,5515585614,102,084
Output od shell script :
,dd,55,5701,7101,7101,1694
,dd,55,1558,5614,5614,0848
Not sure why comma is coming in startign of output, instead as per shell script it should come in the end.
please help
Here is a suggested awk command that should replace all of your shell+awk code. This awk also takes care of trailing \r:
awk -v RS=$'\r' 'BEGIN{FS=OFS=","} NF>3{
startNum=$1; endNum=$2; termPrefix=$4;
if (endNum ~ /^(81|33|55)/) {
areaCode=substr(endNum,1,2); series=substr(endNum,3,4)
}
else {
areaCode=substr(endNum,1,3); series=substr(endNum,4,3)
}
startCLI=substr(startNum,7,4); endCLI=substr(endNum,7,4);
print "Add", areaCode, series, startCLI, endCLI, termPrefix
}' file
Add,55,5701,7101,7101,1694
Add,55,1558,8561,5614,084

./test.ksh[9]: [: argument expected

We are running to script to find the zfs file system monitoring and having error as below.
argument expected
#!/bin/sh
USED_SPACE_PERCENT_WARN=20
PATH=/usr/bin:/usr/sbin; export PATH
# check zfs File system
if [ `df -F zfs | wc -l` -gt 0 ]; then
/usr/sbin/zpool list -H | while read line
do
USED_SPACE_PERCENT=`echo "$line" | nawk -F'[ % ]+' '{print $5}'`
if [ $USED_SPACE_PERCENT -gt $USED_SPACE_PERCENT_WARN ]; then
POOL=`echo "$line" | nawk -F'[ % ]+' '{print $1}'`
echo "ZFS pool $POOL has used $USED_SPACE_PERCENT% of its space."
fi
done
fi
When USED_SPACE_PERCENT is empty (line without 5 fields), the command
if [ $USED_SPACE_PERCENT -gt $USED_SPACE_PERCENT_WARN ]; then
will turn into
if [ -gt 20 ]; then
and that causes the error

Bash error echo a command

I have a problem. I need to show a echo from a while, I use two echo the first one work but the second it give a error.
#!/bin/bash
conexiuni="/tmp/conexiuni"
if [ "$1" != "" ]; then
netstat -tuan | grep $1 | grep ESTAB | awk '{print $5}' | cut -d: -f1 | sort | uniq -c | sort -n > $conexiuni
else
netstat -tuan | grep ESTAB | awk '{print $5}' | cut -d: -f1 | sort | uniq -c | sort -n > $conexiuni
fi
cat $conexiuni | while read line
do
con=`echo ''$line'' | awk '{print $1}'`
ip=`echo ''$line'' | awk '{print $2}'`
if [ "$con" -gt "4" ]; then
`echo -e "$ip" >> /var/log/drop_sc_ip`
`echo -e "$ip"`
fi
done
if [ -f "$conexiuni" ];
then
`rm -rf $conexiuni`
fi
The error is :
./show_conn: line 15: 8.97.80.2: command not found
./show_conn: line 15: 8.76.109.13: command not found
./show_conn: line 15: 8.33.15.2: command not found
./show_conn: line 15: 9.118.226.3: command not found
You can write this part without the backticks:
if [ "$con" -gt "4" ]; then
echo -e "$ip" >> /var/log/drop_sc_ip
echo -e "$ip"
fi
also same in this part:
rm -rf $conexiuni
with the backticks, it first executes what is inside the backticks and then tries to execute the output of the backticks.
and change the loop:
while read con ip
do
if [ "$con" -gt "4" ]; then
echo -e "$ip" >> /var/log/drop_sc_ip
echo -e "$ip"
fi
done < $conexiuni

bash script: commands after the loop are not executed if the loop condition is false

I have a strange behavior in my script bash. when the while condition is true the script behaves correctly but if it's false the commands after the loop aren't executed at all and the script stops. There is no break in my commands after the loop.
I cannot see where is the problem! Any help is welcome :)
Thanks in advance.
while [ expression1 ] || [ expression2 ]
do
echo in the loop
if [ expression3 ] && [ expression4 ] ;
then
commands..
break;
fi
commands..
done
commands..
echo out from the loop
Real code:
start_t=`grep Start_t $job_template | awk -F= '{print $2}'`
current_date=`date +%s`
progress_t=`expr $current_date - $start_t`
exec_t=`grep Exec_t $job_template | awk -F= '{print $2}'`
running_state="r"
req_state $job_id # get the state
xml_state=` grep "job_id=$job_id" $list_job_file | awk '{print $4}'`
while [ $state = $running_state ] || [ $xml_state = "stoped" ]
do
echo in the loop
if [ "$xml_state" = "running" ] && [ $progress_t -gt $exec_t ] ;
then
kill_job $job_id
update_status $job_template "killed"
echo The job is killed
break;
fi
sleep $sleeping_t
$req_state $job_id # to update the state
echo state $state
xml_state=` grep "job_id=$job_id" $list_job_file | awk '{print $4}' `
echo xml_state $xml_state
start_t=`grep Start_t $job_template | awk -F= '{print $2}'`
current_date=`date +%s`
progress_t=`expr $current_date - $start_t`
done
echo out from the loop
commands..
There are many mistakes in this script :
state is not initialised
as test are done with single right bracket, the variables should be double quoted to avoid shell expansion
seems req_state is a function or a command, so there must not be preceded by a $
useless use of grep with awk : grep Start_t $job_template | awk -F= '{print $2}' and awk -F= '/Start_t/{print $2}' $job_template will do the same thing.

Resources