#! /bin/bash
echo "Please Input [a-z]"
#! /bin/bash
# argcount() is function, in objective 9
echo $0
echo $1
echo $2
echo $3
echo $4
echo $5
echo $@
echo $*
argcount() {
echo "Number of arguments: $#"
echo '$1:' "$1"
}
echo 'argcount "$@"'
argcount "$@"
echo 'argcount "$*"'
argcount "$*"
echo OVER
echo "Total argu is $#"
#! /bin/bash
# [] is the test command in objective 4, -f --> file
[ -f /home/joe/dir1 ] && echo "/home/joe/dir1 is a File"
exit
#! /bin/bash
[ -f /home/joe/dir1 ] || touch /home/joe/dir1
ls -l /home/joe
exit
#! /bin/bash
# if is in objective 4,
# -d --> directory
if [ -d /home/joe/dir ]; then
echo "the Dir exist"
exit
else
echo "the Dir non-exist"
mkdir /home/joe/dir
fi
#! /bin/bash
# -e --> exist
if [ -e /home/joe/dir/file1 ]; then
rm -rf /home/joe/dir/file1
echo "File deleted"
elif [ -d /home/joe/dir ]; then
echo "the dir exist"
touch /home/joe/dir/file1
exit
else
echo "the dir non-exist"
mkdir /home/joe/dir
fi
#! /bin/bash
# -n --> nonzero
[ -n $USER -a "$USER" = "root" ] && echo "you are root"
exit
#! /bin/bash
# for is the command in objective 4
for i in $( cat /home/joe/userlist )
do
echo "Add user $i "
useradd -m $i
echo 1234 | passwd --stdin $i
echo "done"
done
#! /bin/bash
for (( i=100 ; i < 120 ; i=i+1 ));
do
echo "Add user x$i"
useradd -m x$i
echo 1234 | passwd --stdin x$i
echo "done"
done
#! /bin/bash
# read is the command in objective 6
echo "Please Input your choice"
read CHOICE
echo "Your choice is $CHOICE"
#! /bin/bash
#let, $(( )), is the command in objective 5
a=100
b=200
c=$a+$b
d=$(( $a+$b ))
echo $c
echo $d
let e=$a+$c
echo $e
#!/bin/bash
#while is the command in objective 4
while [ -n "$1" ]; do
echo "\$1 is $1"
echo "\$2 is $2"
echo "Total arg is $#"
shift
done
#!/bin/bash
#case is the command in objective 4
echo "Enter yes or no"
read yesno
case "$yesno" in
y*|Y*)
echo "Your answer is YES"
;;
[nN]*)
echo "Your answer is NO"
;;
*)
echo "You type wrong word"
esac
#!/bin/bash
# until is the command in objective 4
secret=''
until [ "$secret" = "linux" ]; do
echo -n "Enter your favorite OS name"
read secret
done
echo "I love linux"
#!/bin/bash
# array in objective 7
#set value to array
sample[0]="zero"
sample[1]="one"
sample[2]="three"
#show value
for i in 0 1 2; do
echo "sample[$i]=${sample[$i]}"
done
#!/bin/bash
#sed is the command in objective 10
English="She sales seashells in the seashore, the seashells she sales in the seashore is not seashell I am sure."
echo "$English" | sed 's/sales/buy/'
echo "$English" | sed 's/sales/buy/g'
#!/bin/bash
#tr is the command in objective 10
English="She sales seashells in the seashore, the seashells she sales in the seashore is not seashell I am sure."
result=`echo "$English" | tr 'a-z' 'A-Z' `
echo "\$result is $result"
#!/bin/bash
# cut is the command in objective 10
yourip=`ifconfig | grep Bcast | cut -d : -f 2 | cut -d ' ' -f 1 `
echo "Your IP is $yourip"
沒有留言:
張貼留言