2. My First Sony ^H^H^H hellscript
#!/bin/bash
echo “Hello World!”
$ sh script.sh
Hello World
3. Output redirection
There are 3 file descriptors: Examples:
stdin, stdout and stderr.
1. ls -l > ls-l.txt
Basically you can: 2. grep da * 2> grep-errors.txt
redirect stdout to a file 3. grep da * 1>&2
redirect stderr to a file 4. grep * 2>&1
redirect stdout to a stderr 5. rm -f $(find / -name core) &>
/dev/null
redirect stderr to a stdout
redirect stderr and stdout to a file
redirect stderr and stdout to
stdout
redirect stderr and stdout to stderr
1 'represents' stdout and 2 stderr.
4. Pipes
Pipes can redirect output from one command to
another
Example:
$ cat /etc/passwd | grep „whoami‟
7. Conditions
if [expression]; Examples:
then
code if 'expression' is true. #!/bin/bash
if [ "foo" = "foo" ]; then
fi echo “expression
evaluated as true”
fi
#!/bin/bash
if … then T1="foo"
else T2="bar"
if [ "$T1" = "$T2" ]; then
elseif echo “expression
evaluated as true”
else
echo “expression
evaluated as false”
fi
8. Loops
3 kinds of loops Examples:
#!/bin/bash
for loop for i in `seq 1 10`; do
echo $i
while loop Done
until loop
#!/bin/bash
COUNTER=0
while [ $COUNTER -lt 10 ]; do
echo “The counter is “ $COUNTER
let COUNTER=COUNTER+1
done
9. Assignment:
Write a linux shell script to automagically
create 1000 user accounts. Make sure their
default password is “Hello123”
(hint: you cannot set the pasword with the –p
option….)
10. Useful files and commands
nano, joe, vi (editors)
/etc/passwd
useradd
passwd
rm -rf