Update loops for shell reference

This commit is contained in:
Rico Sta. Cruz 2015-09-09 19:16:33 +08:00
parent 82a1f5d975
commit 6acd0ce490
1 changed files with 23 additions and 6 deletions

29
sh.md
View File

@ -30,12 +30,6 @@ layout: default
${CC:=gcc} # $CC || "gcc" ${CC:=gcc} # $CC || "gcc"
${CC:-gcc} # same as above ${CC:-gcc} # same as above
### Loops
for i in /etc/rc.*; do
echo $i
done
### Reading input ### Reading input
echo -n "Proceed? [y/n]: " echo -n "Proceed? [y/n]: "
@ -44,6 +38,21 @@ layout: default
read -n 1 ans # Just one character read -n 1 ans # Just one character
Loops
-----
### Basic for loop
for i in /etc/rc.*; do
echo $i
done
### Ranges
for i in {1..5}; do
echo "Welcome $i"
done
Functions Functions
--------- ---------
@ -143,6 +152,8 @@ Arrays
echo ${#Fruits[3]} # String length of the Nth element echo ${#Fruits[3]} # String length of the Nth element
echo ${Fruits[@]:3:2} # Range (from position 3, length 2) echo ${Fruits[@]:3:2} # Range (from position 3, length 2)
### Operations
Fruits=("${Fruits[@]}" "Watermelon") # Push Fruits=("${Fruits[@]}" "Watermelon") # Push
Fruits=( ${Fruits[@]/Ap*/} ) # Remove by regex match Fruits=( ${Fruits[@]/Ap*/} ) # Remove by regex match
unset Fruits[2] # Remove one item unset Fruits[2] # Remove one item
@ -150,6 +161,12 @@ Arrays
Fruits=("${Fruits[@]}" "${Veggies[@]}") # Concatenate Fruits=("${Fruits[@]}" "${Veggies[@]}") # Concatenate
lines=(`cat "logfile"`) # Read from file lines=(`cat "logfile"`) # Read from file
### Iteration
for i in "${arrayName[@]}"; do
echo $i
done
Misc crap Misc crap
--------- ---------