Forums

Resolved
0 votes
I just created today my first bash script! I must admit I have some experience with the Apple's Swift programming language but creating bash scripts is new to me. It was fun to create this script and very educational.


#!/bin/bash

# Script to start all containers at once

ALL_CONTAINERS=($(podman ps --all --quiet))

echo ${ALL_CONTAINERS}

for i in ${ALL_CONTAINERS}
do
:
podmanstart $i
done


The script checks for present containers and then start these containers all at once.

I was inspired cause there is a "podman stop --all" command but not a podman start --all command.

I used vim to create this little script.

What are your experiences with Bash scripting?
Tuesday, May 07 2019, 01:53 PM
Share this post:
Responses (6)
  • Accepted Answer

    Friday, May 10 2019, 02:15 PM - #Permalink
    Resolved
    0 votes
    Nick Howitt wrote:

    OK so you don't need the ":". I've had to use it or "true" in places (see /etc/clearos/firewall.d/local or custom). I copied it from custom to local. The reason for needing it there is you cant do "if ... then" or "if ... fi" with nothing in between. There has to be an executable statement, so not a comment. "true" is good enough.


    That is a interesting script because there is no command after the expression you stop the if statement with true. So the condition has to be true to execute the command after then and that is also true if the script is empty?


    I edit in Notepad++ through WinSCP or with WinSCP's own editor, but you'd say I was a Windows luddite! I've never got into linux editors. Just don't be tempted to use a tab instead of the four spaces (if you want perfection).


    Nick why don't you just SSH into your box and do the changes? So much easier!

    The auto indent function of Vim works nicely!
    The reply is currently minimized Show
  • Accepted Answer

    Friday, May 10 2019, 01:12 PM - #Permalink
    Resolved
    0 votes
    OK so you don't need the ":". I've had to use it or "true" in places (see /etc/clearos/firewall.d/local or custom). I copied it from custom to local. The reason for needing it there is you cant do "if ... then" or "if ... fi" with nothing in between. There has to be an executable statement, so not a comment. "true" is good enough.

    I edit in Notepad++ through WinSCP or with WinSCP's own editor, but you'd say I was a Windows luddite! I've never got into linux editors. Just don't be tempted to use a tab instead of the four spaces (if you want perfection).
    The reply is currently minimized Show
  • Accepted Answer

    Friday, May 10 2019, 01:02 PM - #Permalink
    Resolved
    0 votes
    Nick Howitt wrote:

    p.s. What is the ":" doing? Is it just to avoid an empty loop while testing?


    The double colon ":" means that you range has to be true. The colon ":" is the same as true. You can replace the colon with true.

    Something maybe handy is auto indent function of Vim. Do "gg" then "=" then "G". This will indent your code. "gg" = beginning of block, "=" = indent, "G" = end of block.
    The reply is currently minimized Show
  • Accepted Answer

    Thursday, May 09 2019, 10:39 AM - #Permalink
    Resolved
    0 votes
    I think the convention is that way so variables don't ever get the same name as an executable program. But when I started updating scripts in ClearOS I noticed it was all over the place, with some using upper case and some using lower case and maybe even camel case/mixed case. I did ask the devs and was told it was up to me. I am totally inconsistant and try to use the same as any script I am modifying.

    There is another more recent convention with indenting in that you don't use tabs any more. Instead you use four spaces. Again you'll find ClearOS all over the place because of the age of some programs.

    Also in bash you sometimes see ";" at line endings and for joining lines so you might see youe scriptlet like:
    #!/bin/bash

    # Script to start all containers at once

    ALL_CONTAINERS=($(podman ps --all --quiet))

    echo ${ALL_CONTAINERS}

    for i in ${ALL_CONTAINERS}; do
    :
    podmanstart $i
    done
    The internet is all over the place with putting the "do" on the same line as the "for". Some do, some don't. I tend to but I am not an authority

    p.s. What is the ":" doing? Is it just to avoid an empty loop while testing?
    The reply is currently minimized Show
  • Accepted Answer

    Wednesday, May 08 2019, 04:42 PM - #Permalink
    Resolved
    0 votes
    Thanks Nick for the links!

    Learned today that following the naming conventions variables are written uppercase in scripts. Also variables names must have a meaning. It must explain what the variable does. The last convention was something I already knew.
    The reply is currently minimized Show
  • Accepted Answer

    Tuesday, May 07 2019, 02:15 PM - #Permalink
    Resolved
    0 votes
    Google was my friend when I started. I had the following bookmarked:
    http://www.linuxtopia.org/online_books/advanced_bash_scripting_guide/communications.html
    http://ss64.com/bash/
    http://linuxconfig.org/Bash_scripting_Tutorial
    http://www.arachnoid.com/linux/shell_programming.html

    And I printed you a bash manual. As usual I wanted to run before I could walk and I still always have to look up "if" conditionals and single and double "["'s.
    The reply is currently minimized Show
Your Reply