bash script extract domain name for LDAP - bash

I am currently working on a script that asks the user entered a domain name. This domain name is used to fill the hqbase variable for LDAP.
For example, if the user enter "example.com", i have to cut the domaine name with example on the side and "com" another side.
I found this example:
echo http://example.com/index.php | awk -F/ '{print $3}'
But how to split example from com (any ".").

You want . as your delimiter:
# Using cut
$ echo "domain.com" | cut -d. -f1
domain
$ echo "domain.com" | cut -d. -f2
com
# Using awk
$ echo "domain.com" | awk -F. '{print $1}'
domain
$ echo "domain.com" | awk -F. '{print $2}'
com
# Save values
$ first=$(echo "domain.com" | cut -d. -f1)
$ second=$(echo "domain.com" | cut -d. -f1)

Related

Filtered Windows comand works on it's own inside WSL, but not in a script

I have this command which returns an IP successfully:
user#laptop:~$ systeminfo.exe | sed -n '/Connection Name: vEthernet (WSL)/,+4p' | egrep --word-regexp '\[01\]:' | awk '{print $2}'
172.22.0.1
I am trying to concatenate and export an environmental variable DISPLAY using a script with this content:
LOCAL_IP=$(systeminfo.exe | sed -n '/Connection Name: vEthernet (WSL)/,+4p' | egrep --word-regexp '\[01\]:' | awk '{print $2}')
export DISPLAY=$LOCAL_IP:0
But after this script runs, DISPLAY doesn't look like expected:
user#laptop:~$ echo $DISPLAY
:02.22.0.1
I was expecting an answer 172.22.0.1:0. What went wrong?
LOCAL_IP appears to have a trailing \r; od -c <<< "${LOCAL_IP}" should show the value ending in a \r
One fix using parameter substitution:
$ export DISPLAY="${LOCAL_IP//$'\r'/}:0"
$ echo "${DISPLAY}"
172.22.0.1:0
Another option would be to add an additional pipe on the end of OP's current command, a couple ideas (dos2unix, tr -d '\r'); 3rd option modifies the awk script to remove the \r:
systeminfo.exe | sed -n '/Connection Name: vEthernet (WSL)/,+4p' | egrep --word-regexp '\[01\]:' | awk '{print $2}' | dos2unix
# or
systeminfo.exe | sed -n '/Connection Name: vEthernet (WSL)/,+4p' | egrep --word-regexp '\[01\]:' | awk '{print $2}' | tr -d '\r'
# or
systeminfo.exe | sed -n '/Connection Name: vEthernet (WSL)/,+4p' | egrep --word-regexp '\[01\]:' | awk '{gsub(/\r/,"");print $2}'
Another option would be to replace the sed/egrep/awk/tr with a single awk call. If OP wants to go this route I'd recommend asking a new question, making sure to provide the complete output from systeminfo.exe to better understand the parsing requirements.

How to find list of groups that are present in '/etc/passwd' but not in the '/etc/group'?

I want to find list of groups that are present in /etc/passwd but not in the /etc/group
I have written the following command so far
for user in $(getent passwd | cut -d: -f1); do
printf "%s: %s\n" $user "$(id -nG $user)"
done
$ id -Gz | cat -v
197121^#114^#197610^#544^#545^#4^#66049^#11^#15^#113^#4095^#66048^#262154^#405504^#$
$ getent passwd | tr '[:alpha:]' '#'
########:*:197609:197121:#-#######53\########,#-1-5-21-2486228713-2700429697-662227502-1001:/####/########:/###/####
##############:*:544:544:#-#######\##############,#-1-5-32-544:/:/####/#######
######:*:18:18:#-## #########\######,#-1-5-18:/####/######:/###/####
##### #######:*:19:19:#-## #########\##### #######,#-1-5-19:/:/####/#######
####### #######:*:20:20:#-## #########\####### #######,#-1-5-20:/:/####/#######
##############:*:544:544:#-#######\##############,#-1-5-32-544:/:/####/#######
## #######+################:*:328384:328384:#-## #######\################,#-1-5-80-956008885-3418522649-1831038044-1853292631-2271478464:/:/####/#######
$ awk 'NR==FNR{grps[$0];next} !($3 in grps){print $3}' RS='\0' <(id -Gz) RS='\n' FS=':' <(getent passwd)
197609
18
19
20
328384
Could you please try following, where it will give entries of those group names which are present in /etc/passwd and NOT in /etc/group.
awk -F':' 'FNR==NR{a[$5]=$0;next} ($1 in a){delete a[$1]} END{for(i in a){print a[i]}}' /etc/passwd /etc/group
with grep/cut
$ grep -vxFf <(cut -d: -f1 </etc/group) <(cut -d: -f1 </etc/passwd)

Bash Cut text from line with different delimiters

I have a variable with value like:
#capability_ids type="list">[LOADBALANCER]</capability_ids>#
And need to extract from this string type of equipment ( LOADBALANCER ).
I've tried to use cut, but don't know how write cut command with different delimiters.
DeviceType=$( echo $DeviceTypeDirty | cut -d'[' -f1)
Can enywone help me with right solution on bash?
use awk with regular expression: awk -F '[\\[\\]]' '{print $2}'
$ echo '#capability_ids type="list">[L3SWITCH]/capability_ids>#'|awk -F '[\\[\\]]' '{print $2}'
$ L3SWITCH
$ DeviceType=$( echo "$DeviceTypeDirty" | awk -F '[\\[\\]]' '{print $2}')
I tried and got to extract "LOADBALANCER"
Administrators-MacBook-Pro:~$ echo "\"list\">[LOADBALANCER]
</capability_ids>#"|awk -F '[][]' '{print $2}'
LOADBALANCER
Administrators-MacBook-Pro:~$
Hope that helps!
Using cut:
DeviceTypeDirty="#capability_ids type="list">[LOADBALANCER]</capability_ids>#"
DeviceType="$(echo "$DeviceTypeDirty" | cut -d'[' -f2 | cut -d']' -f1)"
Output:
echo "$DeviceType"
LOADBALANCER

Bash grep local IP address and write to file in makefile?

I'm trying to get my local ip with ifconfig and write it to file but for some reason fail to do so.
run:
LOCALIP=$(shell ifconfig | grep -Eo 'inet (addr:)?([0-9]*\.){3}[0-9]*' | grep -Eo '([0-9]*\.){3}[0-9]*' | grep -v '127.0.0.1' | awk '{print $1}') &
echo "var LOCAL_IP = '${LOCALIP}'" > local_ip.js &
It does get the correct IP but for some reason fails to write it to file and file contains only var LOCAL_IP = ''
Could anyone help me and tell what I'm doing wrong?
Two issues: one, each separate command in a Makefile recipe is executed in a separate shell, so you need to "merge" them. Two, you need to double the $ in the echo so that it is sent to the shell, not expanded first by make. This should work:
run:
LOCALIP=$(shell ifconfig | grep -Eo 'inet (addr:)?([0-9]*\.){3}[0-9]*' | grep -Eo '([0-9]*\.){3}[0-9]*' | grep -v '127.0.0.1' | awk '{print $1}'); \
echo "var LOCAL_IP = '$${LOCALIP}'" > local_ip.js
The semicolon and the line-continuation backslash ensure that the entire recipe is executed in the same shell, so that LOCALIP is still set when the echo is run. You could "simplify" this by embedding the command substitution directly in the argument to echo.
run:
echo "var LOCAL_IP = '$(shell ifconfig | grep -Eo 'inet (addr:)?([0-9]*\.){3}[0-9]*' | grep -Eo '([0-9]*\.){3}[0-9]*' | grep -v '127.0.0.1' | awk '{print $1}')'" > local_ip.js
This awk can work:
ifconfig | awk -F: '/inet addr/&& !($2 ~ /127\.0\.0\.1/){gsub(/ .*/, "", $2); print $2}'

Print out onto same line with ":" separating variables

I have the following piece of code and would like to display HOST and RESULT side by side with a : separating them.
HOST=`grep pers results.txt | cut -d':' -f2 | awk '{print $1}'`
RESULT=`grep cleanup results.txt | cut -d':' -f2 | awk '{print $1}' | sed -e 's/K/000/' -'s/M/000000/'`
echo ${HOST}${RESULT}
Please can anyone assist with the final command to display these, I am just getting all of hosts and then all of results.
You probably want this:
HOST=( `grep pers results.txt | cut -d':' -f2 | awk '{ print $1 }'` ) #keep the output of the command in an array
RESULT=( `grep cleanup results.txt | cut -d':' -f2 | awk '{ print $1 }' | sed -e 's/K/000/' -'s/M/000000/'` )
for i in "${!HOST[#]}"; do
echo "${HOST[$i]}:${RESULT[$i]}"
done
A version that works without arrays, using an extra file handle to read from 2 sources at at time.
while read host; read result <&3; do
echo "$host:$result"
done < <( grep peers results.txt | cut -d: -f2 | awk '{print $1}' ) \
3< <( grep cleanup results.txt | cut -d':' -f2 | awk '{print $1}' | sed -e 's/K/000/' -'s/M/000000/')
It's still not quite POSIX, as it requires process substitution. You could instead use explicit fifes. (Also, an attempt to shorten the pipelines that produce the hosts and results. It's probably possible to combine this into a single awk command, since you can either do the substitution in awk, or pipe to sed from within awk. But this is all off-topic, so I leave it as an exercise to the reader.)
mkfifo hostsrc
mkfifo resultsrc
awk -F: '/peers/ {split($2, a, ' '); print a[1]}' results.txt > hostsrc &
awk -F: '/cleanup/ {split($2, a, ' '); print a[1]}' results.txt | sed -e 's/K/000' -e 's/M/000000/' > resultsrc &
while read host; read result <&3; do
echo "$host:$result"
done < hostsrc 3< resultsrc

Resources