Difference between revisions of "OPS435 Lecture 8 - Bash"
(→Arrays) |
m (Andrew moved page OPS435 Lecture 8 to OPS435 Lecture 8 - Bash) |
(No difference)
|
Latest revision as of 12:41, 22 August 2017
Arrays
- for (( )) loop
- Arrays
- To get the length of an array: ${#ArrayName[@]}
In-class examples
for.sh:
#!/bin/bash
#for I in `seq 10`
#do
#echo $I
#done
for ((I=0; $I<10; I++))
do
echo $I
done
arrays.sh:
#!/bin/bash
#COLOUR1=red
#COLOUR2=green
#COLOUR3=blue
COLOURS=("red" "light green" "blue" "orange" "indigo")
#echo ${COLOURS[1]}
#echo ${COLOURS[*]}
#for ((INDEX=0; $INDEX<3; INDEX++))
for ((INDEX=${#COLOURS[@]} - 1; $INDEX >= 0; INDEX--))
do
echo "Array value at index $INDEX is ${COLOURS[$INDEX]}"
done
safecopy-shorter.sh:
#!/bin/bash
COPYTODIR=$1
# Not valid characers in a filename:
# < > : " / \ | ? *
isValidFilename()
{
BADCHARS=('<' '>' ':' '"' '/' '\\' '|' '?' '*')
NAME=$1
for ((INDEX=0; $INDEX<${#BADCHARS[@]}; INDEX++))
do
#echo "See if $NAME contains ${BADCHARS[$INDEX]}"
if echo $NAME | grep ${BADCHARS[$INDEX]} > /dev/null
then
return 1
fi
done
return 0
}
for FILE in *
do
if isValidFilename "$FILE"
then
echo "$FILE is good"
else
echo "$FILE is bad"
fi
done
#cp -v $COPYFROMDIR/* $COPYTODIR/