Hello there geeks,
New to Julia, I’m curious to see how much of my shell scripts I will be able to convert to Julia.
I have this script that create user accounts with random passwords
#!/bin/bash
# This script automatically creates user accounts with random passwords.
LOGFILE=/tmp/geekscreated_$(date +%Y%m%d-%H%M).log
# Declare local variables, generate random password.
randompw=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9!*#' | fold -w 9 | head -n 1)
echo 'Adding new DataSiFi greeks Today's date: $(date +%Y%m%d-%H%M) >> $LOGFILE
# Assumes groups are in place
# If not then add these with groupadd -g 1003 kafka -g 1004 datascience -g 1009 sissyadmins
for u in $( cat scifigeeks.txt ); do
useradd -m -g datascience -G kafka,wheel -d /home/$u -c "A DataSiFi Geek" -s /bin/bash $u
echo "userID:" $u "added successfully!"
echo $u:$randompw | chpasswd
echo '=========================================================' >> $LOGFILE
echo "UserID:" $u "has been created with password:" $randompw >> $LOGFILE
echo '<-------------------------------------------------------->' >> $LOGFILE
done
Then, for shell scripts, it depends a bit. For longer running ones, I’d say yes, for fast ones no. Because Julia has too long a start-up time. Having said this, I write most of my “shell scripts” in Julia.
Brilliant @mauro3. I figured that much… just wanted to started with something fairly simple but I think this quick script it’s not a good start example. Took care of the quoting, apologies, rushed out to post, learning my way around Julia forums. Thanks for sharing knowledge…
LOGFILE=/tmp/geekscreated_$(date +%Y%m%d-%H%M).log:
For this, you probably want to use the Dates stdlib, especially the format function:
using Dates
logfile = "/tmp/geekscreated_$(Dates.format(now(), "+Ymd-HM")).log"
(If you want to deal with paths in a more generic and platform-independent way, you might also want to take a look at the joinpath function.)
randompw=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9!*#' | fold -w 9 | head -n 1)
The rand function is great at generating a random sequence from a collection of items (String is to convert an array of characters to a string):
The rest should be fairly straightforward. You might also want to take a look at the Running External Programs section of the manual, if you haven’t already, to run bash commands. For reading and writing to files, Working with Files may also be interesting. Feel free to ask here if you have any further questions.
That said, as @mauro already alluded to, for simple scripts like this, you might not see that many benefits to using Julia over Bash and if you’re running the script over and over a lot, you might also run into issues with compile times. It’s great though, if you want to write something a bit more advanced than just combining a few shell commands!