This question is a sequel of sorts to my earlier question. The users on this site kindly helped me determine how to write a bash for loop that iterates over string values. For example, suppose that a loop control variable fname iterates over the strings "a.txt" "b.txt" "c.txt". I would like to echo "yes!" when fname has the value "a.txt" or "c.txt", and echo "no!" otherwise.
I am some new to the programming world but starting to get hang of it.
My question is,what shell should I use bash or sh (Ubuntu uses dash for sh)
Is the one faster, users more memory etc?
Example scripts
Code:
#1
for I in {1..10}; do echo $I; done
#2
for I in 1 2 3 4 5 6 7 8 9 10; do echo $I; done
#3
for I in $(seq 1 10); do echo $I; done
#4
for ((I=1; I <= 10 ; I++)
going through advanced bash scripting guide example 3.3 running a loop in background, i found this :
#!/bin/bash
# background-loop.sh
for i in 1 2 3 4 5 6 7 8 9 10 # First loop.
do
echo -n "$i "
done & # Run this loop in background.
# Will sometimes execute after second loop.
echo # This 'echo' sometimes will not display.
for i in 11 12 13 14 15 16 17 18 19 20 # Second loop.
do
echo -n "$i "
if i want to display "aaa" on screen:
(1)$: echo aaa | cat ... works OK
(2)$: echo aaa | ( cat ) ... works OK
(3)$: echo aaa | ( cat & ) ... NOT working
(4)$: ( echo aaa & ) | cat ... works OK
(5)$: echo aaa | ( cat <&0 & ) ... works ok in BASH (but not in SH)
(6)$: echo aaa | ( cat <&3 & ) 3<&0 ...
I have a following bash script I encountered on the web that prints the power set of a given lines of elements.
p() { [ $# -eq 0 ] && echo || (shift; p "$@") |
while read r ; do echo -e "$1 $r\n$r"; done }
after the first && there is echo that does not have any argument.
Here is the code to test it:
p $(echo -e "1\n2\n3")
Ok so I got a little bored and desided to make a fun little bash script that you can use to scare Linux neewbies :)
Here is the code:
Quote:
#!/bin/bash
clear
echo "Formating disk(s): " /dev/sd*
sleep 2
echo "Initialising..."
sleep 2
echo "Are you sure you want to format these disks?"
# Declare variable choice and assign value
I am forming a new question based on an earlier one I posted here in regards to a BASH Script I am writing.
Hi.
I'm new to this forum, my English perhaps is not so good, but here is my question:
In bash you can use [[ ]] for tests, and how I understand it the variable names should be expanded automatically.
I have piped line in bash script and want to check if pipe has data, before feeding program
Searching I found about test -t 0 but it doesn't work here.