# Converting from Shell to Julia

**URL:** <https://discourse.julialang.org/t/converting-from-shell-to-julia/41531>\
**Category:** Teaching & Outreach\
**Tags:** question\
**Created:** [June 16, 2020, 4:04pm UTC](https://discourse.julialang.org/t/converting-from-shell-to-julia/41531 "2020-06-16T16:04:36Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![siralfbaez](https://avatars.discourse-cdn.com/v4/letter/s/7feea3/32.png) [@siralfbaez](https://discourse.julialang.org/u/siralfbaez)\
**Post date:** [June 16, 2020, 4:04pm UTC](https://discourse.julialang.org/t/converting-from-shell-to-julia/41531/1 "2020-06-16T16:04:36Z")

</div>

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

```julia
#!/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

```

---

<div class="post-metadata">

**Author:** ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)\
**Post date:** [June 16, 2020, 8:34pm UTC](https://discourse.julialang.org/t/converting-from-shell-to-julia/41531/2 "2020-06-16T20:34:58Z")

</div>

Welcome to Julia!

First, please quote your code to make it readable, see [Please read: make it easier to help you](https://discourse.julialang.org/t/psa-make-it-easier-to-help-you/14757)

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.

---

<div class="post-metadata">

**Author:** ![siralfbaez](https://avatars.discourse-cdn.com/v4/letter/s/7feea3/32.png) [@siralfbaez](https://discourse.julialang.org/u/siralfbaez)\
**Post date:** [June 16, 2020, 8:52pm UTC](https://discourse.julialang.org/t/converting-from-shell-to-julia/41531/3 "2020-06-16T20:52:59Z")

</div>

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…

---

<div class="post-metadata">

**Author:** ![simeonschaub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simeonschaub/32/216566_2.png) [@simeonschaub](https://discourse.julialang.org/u/simeonschaub)\
**Post date:** [June 16, 2020, 10:49pm UTC](https://discourse.julialang.org/t/converting-from-shell-to-julia/41531/4 "2020-06-16T22:49:31Z")

</div>

Some tips to get you started:

1. `LOGFILE=/tmp/geekscreated_$(date +%Y%m%d-%H%M).log`:  
For this, you probably want to use the `Dates` stdlib, especially the `format` function:

```julia
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.)

1. `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):

```julia
random_pw = String(rand(union('a':'z', 'A':'Z', '0':'9', "!*#"), 9))

```

The rest should be fairly straightforward. You might also want to take a look at the [Running External Programs](https://docs.julialang.org/en/v1/manual/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](https://docs.julialang.org/en/v1/manual/networking-and-streams/#Working-with-Files-1) 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!

---

<div class="post-metadata">

**Author:** ![dlakelan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlakelan/32/8491_2.png) [@dlakelan](https://discourse.julialang.org/u/dlakelan)\
**Post date:** [June 17, 2020, 12:41am UTC](https://discourse.julialang.org/t/converting-from-shell-to-julia/41531/5 "2020-06-17T00:41:33Z")

</div>

For generating passwords, the Julia rand function is not a good idea, as it uses a non-cryptographic RNG. You could read from /dev/urandom,

```julia
fh = open("/dev/urandom","r")
val = read(fh,Int64)

```

And use some function to convert int64 into a password, or maybe read a series of UInt8 and convert character by character.

---

<div class="post-metadata">

**Author:** ![siralfbaez](https://avatars.discourse-cdn.com/v4/letter/s/7feea3/32.png) [@siralfbaez](https://discourse.julialang.org/u/siralfbaez)\
**Post date:** [June 17, 2020, 2:52am UTC](https://discourse.julialang.org/t/converting-from-shell-to-julia/41531/6 "2020-06-17T02:52:40Z")

</div>

Brilliant @simeonschaub this a great starting point for me. Thanks for showing me the way.

---

<div class="post-metadata">

**Author:** ![siralfbaez](https://avatars.discourse-cdn.com/v4/letter/s/7feea3/32.png) [@siralfbaez](https://discourse.julialang.org/u/siralfbaez)\
**Post date:** [June 17, 2020, 2:54am UTC](https://discourse.julialang.org/t/converting-from-shell-to-julia/41531/7 "2020-06-17T02:54:23Z")

</div>

Thanks @dlakelan
