I am new to Scripting, and I am not sure what is wrong with mine. I have not yet added groupadd or useradd commands to actually add the user yet, but I will once I get this part working. So far I have:
!#/bin/bash
if [$# -ne 0 ]
then
echo "usage: $0 < file'
exit 1
fi
first=cut -f 1 -d ',' user_list
last=cut -f 2 -d ',' user_list
lastl=cut -f 2 -d ',' user_list | head -c 1
usern=$first $lastl | tr 'A-Z' 'a-z'
tname=$first $last
while read line; do
echo "adding $tname : $usern\n"
done < user_text
The output should look like adding Jet Black: jetb
but its kind of all over the place. Any help or hints as to what I'm doing wrong would help a lot.
You could probably make it a lot simpler by reading up on the role of the IFS
variable in word splitting, then writing something like this:
while IFS=, read first last
do
usern=$(echo "${first}${last:0:1}" | tr [:upper:] [:lower:])
tname="${first} ${last}"
echo "adding ${tname} : ${usern}"
done
Some other things in there to research - substring expansion (${last:0:1}
), character classes for tr
([:upper:]
, etc.), capturing output from commands ($(...)
).
This is a syntactically correct version of your script:
#!/bin/bash
if [ $# -ne 0 ]
then
echo "usage: $0 < file"
exit 1
fi
while read user_list
do
first=`echo $user_list | cut -f 1 -d ','`
last=`echo $user_list | cut -f 2 -d ','`
lastl=`echo $last | head -c 1`
usern=`echo $first $lastl | tr 'A-Z' 'a-z'`
tname=`echo $first $last`
echo "adding $tname : $usern\n"
done < user_text
I think you want to delete the space between $first
and $lastl
in $usern
, like this:
usern=`echo ${first}${lastl} | tr 'A-Z' 'a-z'`
And then there are lots of improvements to do, probably like twalberg said, but I think your best first step is to understand your syntax errors.