Math
$ x=0; let x+=3; let x-=1; echo $x
2
Generate range of numbers
$ for i in {1..5}; do echo -n $i; done
12345
Alternatively
$ for i in $(seq 1 5); do echo $i; done
12345
2
Generate range of numbers
$ for i in {1..5}; do echo -n $i; done
12345
Alternatively
$ for i in $(seq 1 5); do echo $i; done
12345
Iteration, etc...
Change password expiration data
$ chage -d now user
Printf
$ printf "%02d %02d\n" 5 6
05 06
05 06
Echo list of folder names
$ echo */
home/ Desktop/ ...
Iterating through a file line by line
IN_FILE=$1
OUT_FILE=$2
<!-- Add a generated password to end of line -->
while read s; do
echo "$s, `apg -n 1`" >> $OUT_FILE
done < $IN_FILE
OUT_FILE=$2
<!-- Add a generated password to end of line -->
while read s; do
echo "$s, `apg -n 1`" >> $OUT_FILE
done < $IN_FILE
Or as a one liner (even works if lines have spaces)
$ while read s; do echo "$s"; done < broke.txt
Dereference variables
$ var1='some text'
$ var2='var1'
$ echo ${!var2}
some text
$ var2='var1'
$ echo ${!var2}
some text
String and array manipulation
From tldp.org we have two articles covering similar things named Issue18 and string-manipulation:
Creating an array and indexing into arrays:
$ arr=(hi there dude)
$ echo "Middle value: ${arr[1]}"
$ echo "Middle value: ${arr[1]}"
Middle value: there
$ echo ${arr[*]}
hi there dude
$ echo ${arr[*]}
hi there dude
Indexing into strings: ${variable_name:start_position:length}
$ var="Hello" && echo ${var:1:3}
ell
Separate a path and its base filename.
$ FILE="/some/file/with/path.txt"$ basename $FILE
path.txt
$ dirname $FILE
/some/file/with
Another way to do this (the first doesn't always seem to work)
path.txt
No comments:
Post a Comment