If you were to rename the 'while ' loop, what would be it and why?
Answers
Answer:
There's nothing wrong with using a while loop here. You just have to do it right:
set -- *.jpeg
while (($#)); do
mv -- "${1}" "${1/DSC/Paris}"
shift
done
The while loop above is just as reliable as the for loop (it will work with any file names) and while the latter is - in many instances - the most appropriate tool to use, the former is a valid alternative1 that has its uses (e.g. the above could process three files at a time or process only a certain number of arguments etc).
All these commands (set, while..do..done and shift) are documented in the shell manual and their names are self-explanatory...
set -- *.jpeg
# set the positional arguments, i.e. whatever that *.jpeg glob expands to
while (($#)); do
# execute the 'do...' as long as the 'while condition' returns a zero exit status
# the condition here being (($#)) which is arithmetic evaluation - the return
# status is 0 if the arithmetic value of the expression is non-zero; since $#
# holds the number of positional parameters then 'while (($#)); do' means run the
# commands as long as there are positional parameters (i.e. file names)
mv -- "${1}" "${1/DSC/Paris}"
# this renames the current file in the list
shift
# this actually takes a parameter - if it's missing it defaults to '1' so it's
# the same as writing 'shift 1' - it effectively removes $1 (the first positional
# argument) from the list so $2 becomes $1, $3 becomes $2 and so on...
done
Answer:
When two or more computers are connected together so they can communicate with one another, they form a network. ... The Web is a series of interconnected documents stored on a computer somewhere called a site or web