awk field separator using ":" and ignore spaces within ":" - linux

I'm trying to automate the user creation(Bulk Users) in Linux. So, I've created a text file with all the needed parameters for user's creation.
The Text file contains the below content:
tom:tom1:588:0:test user1:/home/test:/bin/false
harry:harry1:589:0:test test2:/hom/test2:/bin/nologin
Trying to use awk command to print the column's in for loop and create the users with the information's from /tmp/user.txt
for userdetails in $(cat /tmp/user.txt)
do
user=`echo $userdetails | awk -v FS=: '{print $1}'`
passwd=`echo $userdetails | awk -v FS=: '{print $2}'`
uid=`echo $userdetails | awk -v FS=: '{print $3}'`
gid=`echo $userdetails | awk -v FS=: '{print $4}'`
comment=`echo $userdetails | awk -v FS=: '{print $5}'`
home_dir=`echo $userdetails | awk -v FS=: '{print $6}'`
user_shell=`echo $userdetails | awk -v FS=: '{print $7}'`
useradd -d "$home_dir" -c "$comment" -s "$user_shell" -u "$uid" -g "$gid" "$user"
echo "$passwd" | passwd "$user" --stdin;
done
Actual Output:
useradd: invalid home directory ''
passwd: Unknown user name 'tom'.
useradd: invalid home directory ''
passwd: Unknown user name 'user1'.
useradd: invalid home directory ''
passwd: Unknown user name 'harry'.
useradd: invalid home directory ''
passwd: Unknown user name 'test2'.
What I'm doing wrong ?
P.S : I am aware there is a command called newusers in Linux, but i need to check the same via script to create bulk users in Linux.

idk how you got started on that track but you're off-base. Just use a shell loop:
while IFS=':' read -r user passwd uid gid comment home_dir user_shell; do
useradd -d "$home_dir" -c "$comment" -s "$user_shell" -u "$uid" -g "$gid" "$user"
echo "$passwd" | passwd "$user" --stdin;
done < /tmp/user.txt
The above is just showing how to read the file contents into variables, it assumes you know what you're doing with the "useradd" and "passwd" lines.

I would use while for read a file by line so the entire line will be stored in the variable you choose( in my case: line) . Then you could echo variable and awk will do the rest of the work. I prefer $() than `` so I use both for now. For this much data you could create a loop into the while to make code sorter and better manageable.
cat /tmp/user.txt | while read line
do
user=`echo $line | awk -F ":" '{print $1}'`
passwd=$(echo $line | awk -F ":" '{print $2}')
uid=$(echo $line | awk -F ":" '{print $3}')
gid=$(echo $line | awk -F ":" '{print $4}')
comment=$(echo $line | awk -F ":" '{print $5}')
home_dir=$(echo $line | awk -F ":" '{print $6}')
user_shell=$(echo $line | awk -F ":" '{print $7}')
useradd -d "$home_dir" -c "$comment" -s "$user_shell" -u "$uid" -g "$gid" "$user"
echo "$passwd" | passwd "$user" --stdin;
done

If you are stuck with awk, you can use Awk Global substitution:
awk '{gsub(/:/,"-")} 1' your_file
This is also feasible using sed with the g flag ( Sed Global substitution ):
sed "s/:/-/g" your_file
In both casesn your edited lines would be printed to the screen.

Related

Awk: parse node names out of "40*r13n15:40*r10n61:40*r11n18:40*r09n15"

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.

How to direct linux bash script output to one line per host

I'd like to change my script(s) so that the command output is separated by a comma and is all on one line per host. Any ideas on how I can achieve this:
1st Script:
#!/bin/bash
for i in `cat ~/hostnames.txt`
do
ssh -q $i 'bash -s' < server_info.sh
done
2nd Script (server_info.sh):
#!/bin/bash
echo -n "Name: "
uname -n
echo -n "CPUs: "
cat /proc/cpuinfo* | grep processor | wc -l
echo -n "Memory (kb): "
cat /proc/meminfo | grep MemTotal: | awk '{print $2}'
echo -n "Current Kernel: "
uname -a | awk '{print $3}'
echo -n "IP: "
hostname -i
echo -e
Changing your 1st script:
#!/bin/bash
for i in cat ~/hostnames.txt
do
ssh -q $i 'bash -s' < server_info.sh
done | awk -v RS= '{$1=$1}1'
Note: Your server_info.sh can be a lot more optimized.For example:
cat /proc/meminfo | grep MemTotal: | awk '{print $2}'
could be changed to:
awk '/MemTotal:/{print $2}' /proc/meminfo

Searching a specific file system in bash

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

Echo result from Linux file with dynamic filename

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]

Getting error while running script to find disk space

I am running below script:-
#!/bin/bash
threshold="20"
i=2
result=`df -kh |grep -v “Filesystem” | awk ‘{ print $5 }’ | sed ‘s/%//g’`
for percent in $result; do
if ((percent > threshold))
then
partition=`df -kh | head -$i | tail -1| awk ‘{print $1}’`
echo “$partition at $(hostname -f) is ${percent}% full”
fi
let i=$i+1
done
But I get the following error:
awk: ‘{
awk: ^ invalid char '▒' in expression
sed: -e expression #1, char 1: unknown command: `▒'
Please help me to resolve this.
What awk does not work? (your script does work fine on my Ubuntu)
This line:
result=`df -kh |grep -v "Filesystem" | awk '{ print $5 }' | sed 's/%//g'`
could be changed to:
result=$(df -kh | awk '!/Filesystem/ {print $5+0}')
Avoid using old and outdated backtics if parentheses works like this: var=$(code...)
This:
partition=`df -kh | head -$i | tail -1| awk '{print $1}'`
could be changed to:
partition=$(df -kh | awk -v line="$i" 'NR==line {print $1}')
This
let i=$i+1
could be change to:
((i++))
This would then give some like this:
#!/bin/bash
threshold="20"
i=2
result=$(df -kh | awk '!/Filesystem/ {print $5+0}')
for percent in $result; do
if ((percent > threshold))
then
partition=$(df -kh | awk -v line="$i" 'NR==line {print $1}')
echo "$partition at $(hostname -f) is ${percent}% full"
fi
((i++))
done
You're using ‘ for a single quote not '. Try re-encoding your file with an editor.
You got the answer to your syntax error, now re-write the whole script as just:
#!/bin/bash
df -kh |
awk -v t=20 -v h="$(hostname -f)" '(NR>1)&&($5+0>t){printf "%s at %s is %s full\n",$1,h,$5}'

Resources