I am a newbie to linux scripting and I am getting an unexpected error. I have made a script which takes into account two options 1 and 2. I am having issues with option 2 I want to extract the usernames from a text file and add all of the users except the EOF to the home directory like this:
Try this for parsing your input file:
if [ -e $Path ]
then
while read user
do
[[ $user = \#* ]] && continue
Username=`echo $user | cut -f2 -d:`
if [ "$Username" != "EOF" ]
then
echo $Username
fi
done < $Path
fi
Related
Problem: I need to create a file where users and passwords are stored. But my script only saves the password of user1.
Example: I explain myself better with an example. When the two users are created with random password, my script assigns the same password on both users.
user1:Eehei5oo8ohz:/home/user1:/bin/bash
user2:Eehei5oo8ohz:/home/user2:/bin/bash
When the result of my script should be something like this:
user1:Eehei5oo8ohz:/home/user1:/bin/bash
user2:Kln2149sdpja:/home/user2:/bin/bash
My script: This is the script I have used:
#!/bin/bash
##Checking if you are root.##
if [ `id -u` -ne 0 ]
then
echo "you dont are a root user."
exit 1
fi
x=`pwgen 12 1`
for i in {1..2}
do
echo "user$i:#:/home/user$i:/bin/bash" >> users.txt
done
for j in $x
do
sed -i "s/#/$j/" users.txt
done
newusers users.txt
users=`cat users.txt`
login=`echo $i | cut -d: -f1` #username
pass=`cat pass.txt | tr " " _`
password1=`echo $i | cut -d: -f2` #password
for in $users
do
echo "$login:$password1" | chpasswd -m
done
rm users.txt
rm pass.txt
I hope I have explained correctly and appreciate all the help.
You could simplify your script and adapt it as follows (I'm referring to the first part only):
#!/bin/bash
# checking if you are root.##
if [ `id -u` -ne 0 ]
then
echo "you are not the root user!"
exit 1
fi
for i in {1..2}
do
x=`pwgen 12 1`
echo "user$i:$x:/home/user$i:/bin/bash" >> users.txt
done
This will create a file users.txt like the one bellow:
user1:ohng3uxohYi9:/home/user1:/bin/bash
user2:Gah5kiehaemi:/home/user2:/bin/bash
I see no point in creating a file with users and then replacing the # sign with a generated password since you can do that from the start!
You can simplify this script by getting rid of sed editing, eg change:
for i in {1..2}
do
echo "user$i:#:/home/user$i:/bin/bash" >> users.txt
done
for j in $x
do
sed -i "s/#/$j/" users.txt
done
to
pass=`cat pass.txt`
i=1
for pw in $pass
do
echo "user$i:$pw:/home/user$i:/bin/bash" >> users.txt
((i++))
done
This way you can get user numbers and passwords in one go and you are guaranteed to get as many usernames as passwords.
I want to pass my n number csv data into command "esusers useradd username -r role -p password". How can i perform this in linux machine. i Have done this window but unable to do in linux machine can anyone help me out of this. My input will contain header also.
csv file user.csv:
user1,role1,pass1
user2,role2,pass2
user3,role3,pass3
The bash script (scripts.sh) to iterate over the csv file:
#!/bin/bash
# Check parameters
if [ "$#" -ne 1 ]; then
>&2 echo "Illegal number of parameters"
exit 1
fi
# Check file
if [ ! -f "${1}" ]; then
>&2 echo "File ${1} not found"
exit 1
fi
FILE="${1}"
while read line; do
USER=`echo ${line} | cut -d"," -f1`
ROLE=`echo ${line} | cut -d"," -f2`
PASS=`echo ${line} | cut -d"," -f3`
echo "adding user ${USER} (role: ${ROLE}) with password: ${PASS}"
esusers useradd "${USER}" -r "${ROLE}" -p "${PASS}"
done < ${FILE}
Then, add execution mode to the script with chmod +x script.sh
and run the script with the csv file as parameter ./script.sh user.csv
$ ./script.sh user.csv
adding user user1 (role: role1) with password: pass1
adding user user2 (role: role2) with password: pass2
adding user user3 (role: role3) with password: pass3
I have written the following Linux shell script through snippits gleaned from the web (I'm very new to shell scripts), its purpose is to ensure only a single instance of a programme is run with the added option of specifying which workspace a programme will open to.
I'm sure much of the code could be better constructed, however it works with one bug, when some, like Thunderbird, are opened they ignore the workspace switch unless the workaround I've added is used, but why? and is there a better way?
The script uses wmctrl: sudo apt-get install wmctrl
Usage: single-switch programme_name [-ws(int)] where (int) is number of workspace (must exist), the -ws param must be the last listed
#!/bin/bash
if ! [ $1 ] ; then exit ; fi
if [ "?" = "$1" ] ; then
FILE=$(echo "$0" | rev | cut -d "/" -f1 | rev) # extract filename from path
echo "usage $FILE <program name> [-ws(int)]"
exit 1;
fi
TITLE=$1
NAME=""
for var in "$#"; do [ "$(echo ${var} | head -c3)" != '-ws' ] && NAME="$NAME $var" ; done # remove our param from command
ntharg() { shift $1 ; echo $1 ; }
PARAM=`ntharg $# "$#"` # get the last param
if [ "-ws" != "$(echo ${PARAM} | head -c3)" ]; then PARAM=-1 ; # check its ours
else
PARAM=$( echo "$PARAM" | egrep -o '[0-9]+' ) # get the number
PARAM=$((PARAM-1)) # decrement
fi
if [ $PARAM -ge 0 ] ; then
wmctrl -x -a "$TITLE" || ( wmctrl -s $PARAM && zenity --title="Launch $TITLE" --warning --text="$TITLE is starting" --timeout="1" ; $NAME )
# dummy message otherwise some (ie thunderbird) ignore switch
else
wmctrl -x -a "$TITLE" || $NAME & # no switch, just raise or run programme
fi
# Done.
#
I'm looking at a away of reading the username and password of a file and inputing those to either add user or delete user.
EG: I have a file named 'userlist' with the following content with this format:
user1 pass1
user2 pass2
user3 pass3
What I don't understand completely is how to use BASH script to add these accounts.
What I have so far is this:
if [[ whoami -ne "root" ]]
then
exit
else
echo "wish to add or delete? a/d"
read uArg
echo "enter file name"
read uFile
if [ $uArg = "a" -o $uArg = "A" ]
then
IDK WHAT TO DO HERE.
elif [ $uArg = "d" -o $uArg = "D" ]
then
IDK WHAT TO DO HERE.
fi
fi
Alright, what I don't understand is how to read each word line by line and input the username and password to add a new user or delete an existing user.
The program is ment to read the whole file and add each user with there corrosponding password. If delete is chosen then it deletes each user within the file.
I'm new to BASH so any help would be greatyl appreciated.
awk perfectly feets your needs.
See this example:
$ awk '{print "Hi! my name is " $1 ", and my pass is " $2}' ./userpass.txt
Hi! my name is user1, and my pass is pass1
Hi! my name is user2, and my pass is pass2
Hi! my name is user3, and my pass is pass3
Awk stores usernames in $1 and passwords in $2 (first and second column).
You can use pipelines to execute the strings you get from awk as commands:
$ awk '{print "echo " $1}' ./userpass.txt | /bin/bash
user1
user2
user3
something along the lines of...
if [[ whoami -ne "root" ]]
then
exit
else
echo "wish to add or delete? a/d"
read uArg
echo "enter file name"
read uFile
if [ $uArg = "a" -o $uArg = "A" ]
then
while read user passwd rest
do
if [ ! -z $rest ]; then
echo "Bad data"
else
useradd -m $user
passwd $user <<EOP
$passwd
$passwd
EOP
fi
done < $uFile
elif [ $uArg = "d" -o $uArg = "D" ]
then
while read user passwd rest
do
if [ ! -z $rest ]; then
echo "Bad data"
else
userdel $user
fi
done < $uFile
fi
fi
First comments:
learn and get used to basic commands like grep, sed, echo, and generally with file manipulation, awk is a good choice as well if you want to know bash basics, a lot you will encounter is about file manipulation
the code could use more error testing, it's just a basic skeleton
careful about string and variables, quote them whenever possible, spaces in strings can do a lot of bad
Could be something along these lines:
echo "wish to add or delete? a/d"
read uArg
echo "enter username"
read uName
grep "^$uName " password-file
RET=$?
if [ "$uArg" == "a" -o "$uArg" == "A" ]
then
[ $RET -eq 0 ] && echo "User is already in file"
if [ $RET -ne 0 ]
then
echo "enter password"
read uPass
echo "$uName $uPass" >> password-file
fi
elif [ "$uArg" == "d" -o "$uArg" == "D" ]
then
[ $RET -ne 0 ] && echo "User is not file"
if [ $RET -eq 0 ]
then
sed -i "/^$uName /d" password-file
echo "User deleted"
fi
fi
I would like to write a shell script in which I'll take a line input from user,which will contain some xxx.cpp as filename.
I want to get that "xxx" in another variable.
e.g.
if user give input as:
some params path/to/file/xyz.cpp more p/ara/ms
I want to get xyz which occurs before".cpp" and after last occurance of "/" before ".cpp"
Use basename [param] [.ext].
echo `basename $1 .cpp`
where 1 is the index of path/to/file.xyz in the argument list.
Here's a sample bash script to prompt the user for a list of files and filter all the input and display only the base name of files that end in '.cpp' (with .cpp removed) and which are readable
#!/bin/bash
echo Enter list of files:
read list_of_files
for file in $(echo $list_of_files); do
if echo $file | grep '\.cpp$' > /dev/null; then
if [[ -r $file ]]; then
fn=$(basename ${file})
fn=${fn%.*}
echo $fn
fi
fi
done