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.
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?
#!/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?
In Water Cooler
Share this post:
Responses (6)
-
Accepted Answer
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! -
Accepted Answer
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). -
Accepted Answer
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. -
Accepted Answer
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:
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#!/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
p.s. What is the ":" doing? Is it just to avoid an empty loop while testing? -
Accepted Answer
-
Accepted Answer
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.
Please login to post a reply
You will need to be logged in to be able to post a reply. Login using the form on the right or register an account if you are new here.
Register Here »