set password for user account from a fileun issue - unix

I want to create user account n times in UNIX
after creating the account I want to set password reads line by line from a file called password.txt
but I do not know how to do that!
my bash script contains this so far:
sudu useradd user
passwd
I have stuck on this point !, I do not know how to set password from a file?
what command should I use?
I want something like this but I want to read pass word not user name
for i in `more pass.txt `
do
echo $i
adduser $i
done

Have the code like this
#!/bin/bash
for i in `cat unpw.csv`; do
UN=`echo $i | cut -f1 -d','`
PW=`echo $i | cut -f2 -d','`
ENCPW=`echo $PW | mkpasswd -s`
echo useradd -p $ENCPW -m $UN
done
where the input file unpw.csv has
user1,password1
user2,password2
user3,password3
PS: Make sure that mkpasswd is installed

Related

Append "/" to end of directory

Completely noob question but, using ls piped to grep, I need to find files or directories that have all capitals in their name, and directories need to have "/" appended to indicate that it is a directory. Trying to append the "/" is the only part I am stuck on. Again, I apologize for the amateur question. I currently have ls | grep [A-Z] and the example out should be: BIRD, DOG, DOGDIR/
It's an interesting question because it's a somewhat difficult thing to accomplish with a bash one-liner.
Here's what I came up with. It doesn't seem very elegant, but I'm not sure how to improve.
find /animals -type d -or -type f \
| grep '/[A-Z]*$' \
| xargs -I + bash -c 'echo -n $(basename +)$( test -d + && echo -n /),\\ ' \
| sed -e 's/, *$//'; echo
I'll break that down for you
find /animals -type d -or -type f writes out, once per line, the directories and files it found in /animals (see below for my test environment dockerfile - I created /animals to match your desired output). Find can't do a regex match as far as I know on the name, so...
grep '/[A-Z]*$' filter's find's output so that only paths are shown where the last part of the file or directory name, after the final /, is all uppercase
xargs -I + bash -c '...' when you're in a shell and you want to use a "for" loop, chances are what you should be using is xargs. Learn it, know it, love it. xargs takes its input, separated by default by $IFS, and runs the command you give it for each piece of input . So this is going to run a bash shell for each path. that passed the grep filter. In my case, -I + will make xargs replace the literal '+' character with its current input filename. -I also makes it pass one at a time through xargs. For more information, see the xargs manual page.
'echo -n $(basename +)$( test -d + && echo -n /),\\ ' this is the inner bash script that will be run by xargs for each path that got through grep.
basename + cuts the directory component off the path; from your example output you don't want eg /animals/DOGDIR/, you want DOGDIR/. basename is the program that trims the directories for us.
test -d + && echo -n / checks to see whether + (remember xargs will replace it with filename) is a directory ,and if so, runs echo -n /. the -n argument to echo suppresses the newline, important to get the output in the CSV format you specified.
now we can put it all together to see that we're echo -n the output of basename + , with / appended, if it's a directory, and then , appended to that. All the echos run with -n to suppress newlines to keep output CSV looking.
| sed -e 's/, *$//'; echo is purely for formatting. Adding , to each individual output was an easy way to get the CSV, but it leaves us with a final , at the end of the list. The sed invocation removes , followed by any number of spaces at the end of the output so far - eg the entire output from all the xargs invocations. And since we never did output a newline at the end of that output, the final echo is adding that.
Usually in unix shells, you probably wouldn't want a CSV style output. You'd probably instead want a newline-separated output in most cases, one matching file per line, and that would be somewhat simpler to do because you wouldn't need all that faffing with -n and , to make it CSV style. But, valid requirement if the need is there.
FROM debian
RUN mkdir -p /animals
WORKDIR /animals
RUN mkdir -p DOGDIR lowerdir && touch DOGDIR/DOG DOGDIR/lowerDOG2 lowerdir/BIRD
ENTRYPOINT [ "/bin/bash" ]
CMD [ "-c" , "find /animals -type d -or -type f | grep '/[A-Z]*$'| xargs -I + bash -c 'echo -n $(basename +)$( test -d + && echo -n /),\\ ' | sed -e 's/, *$//'; echo"]
$ docker run --rm test
BIRD, DOGDIR/, DOG
You can start looking at
ls -F | grep -v "[[:lower:]]"
I did not add something for a comma-seperated line, because this is the wrong method: Parsing ls should be avoided ! It will go wrong for filenames like
I am a terribble filename,
with newlines inside me,
and the ls command combined with grep
will only show the last line
BECAUSE THIS LINE HAS NO LOWERCASE CHARACTERS
To get the files without a pipe, you can use
shopt -s extglob
ls -dp +([[:upper:]])
shopt -u extglob
An explanation of the extglob and uppercase can be found at https://unix.stackexchange.com/a/389071/57293
When you want the output in one line, you can get troubles with filenames that have newlines or commas in its name. You might want something like
# parsing ls, yes wrong and failing for some files
ls -dp +([[:upper:]]) | tr "\n" "," | sed 's/,$/\n/'

Getting the previous working directory value

here is my simple command.
ls -lrth ../ | grep file | awk -F" " -v orig=`cd .. | pwd ` -v sort=`pwd` '{print $NF "," $7"/"$8"/"$9","orig"," sort }'
I'm trying to get the value of my previous path just above my current working directory.
current working directory = /home/PC1/Environment/Test1
what i want to get the value of pwd is /home/PC1/Environment and not want to hardcode it.
i tried to use cd .. | pwd but it still displays my current working directory not my previous working directory
can anyone help? some suggestions would be nice.
Use $(cd .. && pwd). You can also use $(cd - && pwd) to get your previous working directory even if it wasn't the parent of your current one. (In general, you should use $(...) instead of `...` to get command output; the latter interferes with quoting and doesn't nest, so can cause surprising results).
Your cd | pwd runs the cd and the pwd at the same time in different subshells, which is not what you want.

What is the fastest way to copy a large number of file paths mentioned in a text file, from one directory to another

I have a text file that lists a large number of file paths. I need to copy all these files from the source directory (mentioned in the path in the file one every line) to a destination directory.
Currently, the command line I tried is
while read line; do cp $ line dest_dir; done < my_file.txt
This seems to be a bit slow. Is there a way to parallelise this whole thing or speed it up ?
You could try GNU Parallel as follows:
parallel --dry-run -a fileList.txt cp {} destinationDirectory
If you like what it says, remove the --dry-run.
You could do something like the following (in your chosen shell)
#!/bin/bash
BATCHSIZE=2
# **NOTE**: check exists with -f and points at the right place. you might not need this. depends on your own taste for risk.
ln -s `which cp` /tmp/myuniquecpname
# **NOTE**: this sort of thing can have limits in some shells
for i in `cat test.txt`
do
BASENAME="`basename $i`"
echo doing /tmp/myuniquecpname $i test2/$BASENAME &
/tmp/myuniquecpname $i test2/$BASENAME &
COUNT=`ps -ef | grep /tmp/myuniquecpname | grep -v grep | wc -l`
# **NOTE**: maybe need to put a timeout on this loop
until [ $COUNT -lt $BATCHSIZE ]; do
COUNT=`ps -ef | grep /tmp/myuniquecpname | grep -v grep | wc -l`
echo waiting...
sleep 1
done
done

strange echo output

Can anybody explain this behaviour of the bash shell which is driving me nuts
[root#ns1 bin]# export test=`whois -h whois.lacnic.net 187.14.6.108 | grep -i inetnum: | awk '{print $2}'`
[root#ns1 bin]# echo $test
187.12/14
[root#ns1 bin]# echo "iptables -I INPUT -s $test -J DROP"
-J DROP -I INPUT -s 187.12/14
[root#ns1 bin]#
Why is my echo screwed up? It is being changed by the contents of $test.
If you change $test to "ABC" all is fine. Is it related to the slash?
Why is my echo screwed up? It is being changed by the contents of
$test.
Because your test contains a carriage return. Remove it:
test=$(whois -h whois.lacnic.net 187.14.6.108 | grep -i inetnum: | awk '{print $2}' | tr -d '\r')
Your test contains something like
1234567 -I INPUT -s 187.12/14\r-J DROP
which, due to the carriage return, is visible only as
-J DROP -I INPUT -s 187.12/14
The CR moves the cursor to the start-of-line, where it then overwrites previous characters.
You could try
echo "$test" | od -bc
to verify this.
This is almost certainly a carriage return. echo is doing its job correctly and emitting the string to your terminal; the problem is that your terminal is treating a part of the string as a command for it to follow (specifically, a LF character, $'\r', telling it to send the cursor to the beginning of the existing line).
If you want to see the contents of $test in a way which doesn't allow your terminal to interpret escape sequences or other control characters, run the following (note that the %q format string is a bash extension, not available in pure-POSIX systems):
printf '%q\n' "$test"
This will show you the precise contents formatted and escaped for use by the shell, which will be illuminative as to why they are problematic.
To remove $'\r', which is almost certainly the character giving you trouble, you can run the following parameter expansion:
test=${test//$'\r'/}
Unlike solutions requiring piping launching an extra process (such as tr), this happens inside the already-running bash shell, and is thus more efficient.

How to get the logged in user's real name in Unix?

I'm looking to find out the logged in user's real (full name) to avoid having to prompt them for it in an app I'm building. I see the finger command will output a columned list of data that includes this and was wondering if it makes sense to grep through this or is there an easier way? None of the switches for finger that I've found output just the real name. Any thoughts would be much appreciated.
getent passwd `whoami` | cut -d : -f 5
(getent is usually preferable to grepping /etc/passwd).
getent passwd "$USER" | cut -d: -f5 | cut -d, -f1
This first fetches the current user's line from the passwd database (which might also be stored on NIS or LDAP)
In the fetched line, fields are separated by : delimiters. The GECOS entry is the 5th field, thus the first cut extracts that.
The GECOS entry itself is possibly composed of multiple items - separated by , - of which the full name is the first item. That's what the second cut extracts. This also works if the GECOS entry is lacking the commas. In that case the whole entry is the first item.
You can also assign the result to a variable:
fullname=$( getent passwd "$USER" | cut -d: -f5 | cut -d, -f1 )
Or process it further directly:
echo "$( getent passwd "$USER" | cut -d: -f5 | cut -d, -f1 )'s home is $HOME."
cat <<EOF
Hello, $( getent passwd "$USER" | cut -d: -f5 | cut -d, -f1 ).
How are you doing?
EOF
You can use getpwent() to get each successive password entry until you find the one that matches the currently logged in user, then parse the gecos field.
Better, you can use getpwuid() to directly get the entry for the uid of the current user.
In either case,
You have to first get the current user's login name or id, and
There is no guarantee that the gecos field actually contains the user's real full name, or anything at all.
Specific to macOS, there is no getent command; instead you have to use id -F
For macOS and Linux:
if [ "Darwin" = $(uname) ]; then
FULLNAME=$(id -P $USER | awk -F '[:]' '{print $8}')
else
FULLNAME=$(getent passwd $USER | cut -d: -f5 | cut -d, -f1)
fi
echo $FULLNAME
I use
grep "^$USER:" /etc/passwd | awk -F: '{print $5}'
Explanations:
$USER contains the login of the current user
The first part (grep) extract from /etc/passwd the line about that user
The second part (awk) splits this line with separator ':' and prints the 5th component,
which is the full name
If you don't want to rely on $USER being set, you can use that instead:
grep "^`whoami`:" /etc/passwd | awk -F: '{print $5}'
How about trying whoami or logname

Resources