The problem I have is with echo cannot echo e.g: "$prefix_$suffix". This is a assignment for a class in school if that changes things.
I've tried e.g "$prefix _$suffix" but that creates a space between the prefix and suffix
#!bin/bash
read -p "Username prefix: " prefix
read -p "Amount of users: " amount
read -p "Name of file to store, include extension (e.g test.txt): " filename
touch "$filename"
new="$amount"
suffix=0
state=true
while [ state=true ] ; do
#in this function i reverse the user input amount of users so it appears as user 1,2,3 (and so on) in the first line of the text file that is also user input.
if [ "$new" -ge 1 ] ; then
newpass="$(gpg --gen-random --armor 1 12)"
#reversing process, making the suffix start at 1 so user 1 gets assigned suffix 1 for the username and i decrease the "new" variable that gets set to "$amount" so the while loop isn't indefinite
new=`expr "$new" - 1`
suffix=`expr "$suffix" + 1`
echo -n "$prefix" >> "$filename"
echo -n "_$suffix" >> "$filename"
echo -n " " >> "$filename"
echo "$newpass" >> "$filename"
echo -e >> "$filename"
elif [ "$new" -eq 0 ] ; then
break
fi
done
a run of this bash results in 5 lines e.g:
re_1 UlrZW3jB5L9zt6Nf
and so on, depending how many users you choose at the input
however the next task is to create users with the username, in this example re_1 with the password: UlrZW3jB5L9zt6Nf. This is where the clunky echo stuff I've done doesn't work. I tried doing useradd -u "$prefix_$suffix" and "$prefix $suffix" , none of these work since "$prefix$suffix" is treated as one call instead of two and the "$prefix _$suffix" adds one space in between the prefix and suffix which is not acceptable.
Even if this looks very introverted to you, hence i added comments to make it understandable, help is very appreciated.
Feel free to ask question if you do not understand and want to help!
This will do what you want:
echo "${prefix}_${suffix}"
Related
I am trying to use a while loop to repeatedly prompt for a username during user registration until a valid username has been provided (i.e. it has NOT already been claimed).
Unfortunately, I've only been coding in bash for less than 70 hours now, so I wasn't familiar with how to do this. My initial thought was to use "goto" like in Windows batch scripts if there was a problem with the username, but apparently that's not a thing in bash and loops seemed to be the recommended route. I wasn't familiar with loops in bash so I did some research until I found a good answer on SO about how to do this. This isn't the exact post (I can't find it now), but I was looking at questions like this one.
The resulting code looks like this:
echo -e "Now, choose a unique username. No special characters, please."
uniqueuser=0 # Assume a valid username has not been chosen from the get-go
while [ "$uniqueuser" == "0" ]
do
read -p "Username: " username
lusername=`echo $username | sed y/ABCDEFGHIJKLMNOPQRSTUVWXYZ/abcdefghijklmnopqrstuvwxyz/` #lowercase username
if [ "$lusername" == "admin" ] || [ "$lusername" == "administrator" ] || [ "$lusername" == "npstn" ] || [ "$lusername" == "phreak" ] || [ "$lusername" == "guest" ] || [ "$lusername" == "user" ] || [ "$lusername" == "sysop" ]
then
echo -e "That username is reserved. Please pick another username."
else
username=`php /home/com/encode.php "$username"`
available=`curl "https://example.com/redacted/redacted.php?key=$key&type=checkusername&username=$username" --silent`
if [ "$available" == "1" ]
then
uniqueuser=$((1)) # Username is unique and acceptable
else
echo -e "That username is taken. Please choose another one."
fi
fi
done <input.data # so that username variable persists outside of while loop
echo -e "That username is available... or was... now, it's yours!"
echo -e "On this board, we have no annoying password length or complexity requirements. That said, your password cannot be null, nor can it use the plus symbol. Choose wisely!"
When this part of the shell script is reached, I see:
Now, choose a unique username. No special characters, please.
/home/com/redacted.sh: line 4: input.data: No such file or directory
That username is available... or was... now, it's yours!
On this board, we have no annoying password length or complexity requirements. That said, your password cannot be null, nor can it use the plus symbol. Choose wisely!
The reason "input.data" is used is to get the $username variable out from the subshell and back into the main shell. However, that seems to be causing an error. How can I fix this?
You've misread the sources on which you're relying. Redirecting stdin from a file, as in:
count=0
while IFS= read -r line; do
(( ++count )) # or do something else
done < input.data
echo "$count" # will print number of lines in the file input.data
only prevents a subshell from being created when the alternative is redirecting stdin from a pipe. For example, you would indeed lose the content of variables you set in:
# DO NOT DO THIS; this is the practice the other answers were teaching how to avoid.
count=0
cat input.data | while IFS= read -r line; do
(( ++count )) # or otherwise modify data within the loop
done
echo "$count" # will print 0, because the loop was in a subshell
This change is not necessary, and not appropriate, when your input is from stdin and not from a file.
It's the cat input.data | that is a problem, for which < input.data is a replacement. If your original code had no cat input.data |, then you need not use < input.data to replace it.
Demonstrating that a while read loop can read from stdin, and retain its variables' values, without any such redirection:
count=0
prompt='Enter a string, or a blank line to stop: '
while IFS= read -r -p "$prompt" line && [[ $line ]]; do
(( ++count ))
printf 'Line %d: %s\n' "$count" "$line"
done
echo "Final counter is $count"
...keeps the same value for $count outside the loop it hand inside the loop, when a blank line is entered.
I am creating a simple phonebook using unix shell scripts. I have gotten all of my functions to work except the removal of a contact after it has been created. I have tried combining grep and sed in order to accomplish this, but cannot seem to get over the hump. The removal shell i've tried is as follows.
#!/bin/sh
#removeContact.sh
echo “Remove Submenu”
echo “Please input First Name:”
read nameFirst
echo “Please input Last Name:”
read nameLast
x=$(grep -e “$nameFirst” -e “$nameLast” ContactList)
echo $x
sed '/'$x'/ d' ContactList;
echo “$nameFirst $nameLast is removed from your contacts”
exit 0
I'm not sure if I am declaring x incorrectly, or if my syntax is wrong when sed is used.
Any help would be greatly appreciated. Thank you.
#!/bin/bash
ContactList="contacts.txt"
export ContactList
exit=0
while [ $exit -ne 1 ]
do
echo "Main Menu"
echo "(a) Add a Contact"
echo "(r) Remove a Contact"
echo "(s) Search a Contact"
echo "(d) Display All Contact’s Information"
echo "(e) Exit"
echo "Your Choice?"
read choice
if [ "$choice" = "a" ]
then
./addContact.sh
elif [ "$choice" = "r" ]
then
./removeContact.sh
elif [ "$choice" = "s" ]
then
./searchContact.sh
elif [ "$choice" = "d" ]
then
./displayContact.sh
elif [ "$choice" = "e" ]
then
exit=1
else
echo "Error"
sleep 2
fi
done
exit 0
#!/bin/sh
#addContact.sh
ContactList="contacts.txt"
echo “Please input First Name:”
read nameFirst
echo “Please input Last Name:”
read nameLast
echo “Please input Phone Number:”
read number
echo “Please Input Address”
read address
echo “Please input Email:”
read email
echo $nameFirst:$nameLast:$number:$address:$email>> ContactList;
echo "A new contact is added to your book."
exit 0
sed '/'$x'/ d' ContactList
won't remove anything from the file ContactList, it will simply output the changes to standard output.
If you want to edit the file in-place, you'll need the -i flag (easy) or to make a temporary file which is then copied back over ContactList (not so easy, but needed if your sed has no in-place editing option).
In addition, since ContactList is a shell variable referencing the real file contacts.txt, you'll need to use $ContactList.
And, as a final note, since you're using the full line content to do deletion, the presence of an address like 1/15 Station St is going to royally screw up your sed command by virtue of the fact it contains the / character.
I would suggest using awk rather than sed for this task since it's much better suited to field-based data. With the record layout:
$nameFirst:$nameLast:$number:$address:$email
you could remove an entry with something like (including my patented paranoid perfect protection policy):
cp contacts.txt contacts.txt.$(date +%Y.%m.%d.%H.%M.%S_$$)
awk <contacts.txt >tmp.$$ -F: "-vF=$nameFirst" "-vL=$nameLast" '
F != $1 || L != $2 {print}'
mv tmp.$$ contacts.txt
I am trying to validate multiple inputs separated by spaces(two disk names in the below case) with a shell script. But, I am un-successful in doing so. Can someone help me?
read DISK
if [ "${1}" = "" ] || [ "${2}" = "" ]
then
printf "The Disk pairs cannot be left blank. Exiting script!!!"
exit 1
else
TMP=$DISK
printf "The disks entered are $TMP"
fi
For ksh93, you can use
read -A disks
if [[ ${#disks[#]} -ne 2 ]]; then
print -u2 "You need to enter 2 disks"
exit 1
else
print "You entered: ${disks[*]}"
fi
For ksh88, use the positional parameters
read disks
set -- $disks
if [[ $# -ne 2 ]]; then
print -u2 "You need to enter 2 disks"
exit 1
else
print "You entered: $disks"
fi
The variables ${1} and ${2} are the commandline parameters and are unrelated to the last read command. There are different ways to use the DISK variable.
Once you have the DISK variable read, I would have chosen for a solution like
echo "${DISK}" | while read disk1 disk2 otherfields; do
echo "disk1=${disk1}, disk2=${disk2}"
done
# or better
disk1="${DISK% *}"; echo "${disk1}"
disk2="${DISK#* }"; echo "${disk2}"
# or worse
disk1=$(echo "${DISK}" | cut -d" " -f1)
disk2=$(echo "${DISK}" | cut -d" " -f2)
When you already know you want to split the fields, you can change your first read command. Replace read DISK with
read disk1 disk2 remaining_input
I have to write a shell script that creates a file containing the name of each text files from a folder (given as parameter) and it's subfolders that contain words longer than n characters (read n from keyboard).
I wrote the following code so far :
#!/bin/bash
Verifies if the first given parameter is a folder:
if [ ! -d $1 ]
then echo $1 is not a directory\!
exit 1
fi
Reading n
echo -n "Give the number n: "
read n
echo "You entered: $n"
Destination where to write the name of the files:
destinatie="destinatie"
the actual part that i think it makes me problems:
nr=0;
#while read line;
#do
for fisier in `find $1 -type f`
do
counter=0
for word in $(<$fisier);
do
file=`basename "$fisier"`
length=`expr length $word`
echo "$length"
if [ $length -gt $n ];
then counter=$(($counter+1))
fi
done
if [ $counter -gt $nr ];
then echo "$file" >> $destinatie
fi
done
break
done
exit
The script works but it does a few more steps that i don't need.It seems like it reads some files more than 1 time. If anyone can help me please?
Does this help?
egrep -lr "\w{$n,}" $1/* >$destinatie
Some explanation:
\w means: a character that words consist of
{$n,} means: number of consecutive characters is at least $n
Option -l lists files and does not print the grepped text and -r performs a recursive scan on your directory in $1
Edit:
a bit more complete version around the egrep command:
#!/bin/bash
die() { echo "$#" 1>&2 ; exit 1; }
[ -z "$1" ] && die "which directory to scan?"
dir="$1"
[ -d "$dir" ] || die "$dir isn't a directory"
echo -n "Give the number n: "
read n
echo "You entered: $n"
[ $n -le 0 ] && die "the number should be > 0"
destinatie="destinatie"
egrep -lr "\w{$n,}" "$dir"/* | while read f; do basename "$f"; done >$destinatie
This code has syntax errors, probably leftovers from your commented-out while loop: It would be best to remove the last 3 lines: done causes the error, break and exit are unnecessary as there is nothing to break out from and the program always terminates at its end.
The program appears to output files multiple times because you just append to $destinatie. You could simply delete that file when you start:
rm "$destinatie"
You echo the numbers to stdout (echo "$length") and the file names to $destinatie (echo "$file" >> $destinatie). I do not know if that is intentional.
I found the problem.The problem was the directory in which i was searching.Because i worked on the files from the direcotry and modified them , it seems that there remained some files which were not displayed in file explorer but the script would find them.i created another directory and i gived it as parameter and it works. Thank you for your answers
.
I'm writing a script which is supposed to automatically change test users passwords.
I have a dictionary file which includes all the relevant passwords I'm bound to use and I'm trying to understand how to pull some random passwords from the file in order to set the new passwords and document which test user got which password... I'm having a hard time to find the right pattern I should use, can you please assist?
Thanks in advance
If your shell supports $RANDOM (bash does) and the allowed passwords are contained, one password per line, in a file whose name is paswd you can try
new_pass=$(awk NR==$RANDOM'%'`wc -l paswd| cut -d\ -f1`'+1' paswd)
Example using a different file
for x in {1..5} ; do
> a=$(mawk NR==$RANDOM'%'`wc -l .bashrc| cut -d\ -f1`'+1' .bashrc)
> echo $x "$a"
> done
1 muvi ${1}"$num" && break
2 $1/* ; }
3 alias acse='apt-cache search'
4 xc () { export cnt=`expr $cnt + 1` ; u=$1; shift ; x $lett`printf "%4.4d" $cnt` $u -1 $* ; }
5 alias logout='xfce4-session-logout --logout'
%
The following approach will work for a modestly sized password dictionary.
read the passwords into a bash array
start a while loop
prompt for the user's login-id
read the id
use $RANDOM to generate an index into the array
pull out the password at that position (you'll have to use some mod arithmetic)
use passwd command to change the password (check the man page)
log the user and either the number or the passwd as you desire
back to the start of the loop
You can exit the loop based on something like an empty name being entered.
I hope that's of some help.
06.11.2014
I've editted this answer with a complete, if somewhat unspectacular solution. This is for anyone who, like me, has difficulty understanding the brevity of the preferred solution. Points to note:
In bash you have to declare an array with declare -a YourArray
You should also specify a dedicated index variable: arrIdx=0 for instance
Bash arrays are dynamic so you don't need to declare its size.
Here's the whole thing:
#!/bin/bash
PRG=`basename $0`
if [ $# -lt 2 ]; then
echo "usage $PRG password_file change_log"
exit -1
fi
INFILE=$1
LOGFILE=$2
declare -a passwdArr
arrIdx=0
newPasswd=
for inputPasswd in `cat $INFILE`; do
passwdArr[$arrIdx]=$inputPasswd
echo "inputPasswd = $inputPasswd, arrIdx = $arrIdx, passwdArr[arrIdx] = ${passwdArr[$arrIdx]}"
((arrIdx += 1)) # double parentheses ensure arithmetic evaluation
done
arrSize=$arrIdx # one more than the mximum index
USER="xxx"
while [ "$USER" != "" ]; do
echo "enter user ID:"
read USER
if [ "$USER" != "" ]; then # Do nothing in this case
arrIdx=`expr $RANDOM % $arrSize`
echo "arrIdx = $arrIdx"
newPasswd=${passwdArr[$arrIdx]}
echo "$arrIdx $USER $newPasswd" >> $LOGFILE
echo -e "$newPasswd\n$newPasswd" | passwd $USER
fi
done
Important... the test if [ "$USER" != "" ] within the loop is required in order to avoid inadvertently changing the password of the administrator running the script!