Tải bản đầy đủ

Bài tập windows power shell

Find out if user exists in /etc/passwd file
/etc/passwd file stores essential information required during login. All you have to do is search
this file for user name using following syntax:
$ egrep -i "^username" /etc/passwd

For, example find out if vivek user exists or not, enter:
$ egrep -i "^vivek" /etc/passwd

Sample output:
vivek:x:1000:1000:Vivek Gite,,,,:/home/vivek:/bin/bash

A quick shell script code:
#!/bin/bash
# init
USERID="$1"
#....
/bin/egrep -i "^${USERID}" /etc/passwd
if [ $? -eq 0 ]; then
echo "User $USERID exists in /etc/passwd"
else
echo "User $USERID does not exists in /etc/passwd"

fi
# ....

Normally, exit status is 0 returned if user accounts (lines) are found and 1 otherwise.

Find out if group exists in /etc/group file
/etc/group is an text file which defines the groups to which users belong under Linux and UNIX
operating system. Again, you have to search /etc/group file using following syntax:
$ egrep -i "^groupname" /etc/group

For, example find out if vivek group exists or not, enter:
$ egrep -i "^vivek" /etc/group

id command
id is another command to display user / group information for any USERNAME, or the current
user. To find out more about user called, tom, enter:
$ id tom

Sample output
id: tom: No such user

To find out ftpuser group, enter:
$ id -g ftpuser

Sample output:
1


id: ftpuser: No such user

id command exit status is 0 returned if user accounts (lines) are found and 1 otherwise. A sample
shell script using id command:
#!/bin/bash
USERID="$1"
/bin/id $USERID 2>/dev/null
[ $? -eq 0 ] && echo "User found" || echo "User not found"
/bin/id -g $USERID 2>/dev/null
[ $? -eq 0 ] && echo "Group found" || echo "Group not found"

Bai6a


#!/bin/bash
echo -n "nhap n:"
read n
for (( i = 1;i <= $n; i++ ))
do
for (( j = 0;j < $n-i; j++ ))
do
echo -n " "
done
for (( k = $n-i;k < $n; k++ ))
do
echo -n "*"
done
echo ""
done
6b
#!/bin/bash
echo -n "nhap n:"
read n
for (( i = 1;i <= $n; i++ ))
do
for (( j = 0;j < $n-i; j++ ))
do
echo -n " "
2


done
for (( k = $n-i;k < $n; k++ ))
do
echo -n "$i"
done
echo ""
done
6c
#!/bin/bash
echo -n "nhap n:"
read n
for (( i = 1;i <= $n; i++ ))
do
for (( j = 0;j < $n-i; j++ ))
do
echo -n " "
done
#for (( k = $n-i;k < $n; k++ ))
#do
for (( m = 1; m <= i ; m++))
do
echo -n "$m"
done
#done
echo ""
done
#!/bin/bash
menu()
{
echo "1 ) UNIX"
echo "2) Solaris"
echo "3) Free BSD"
echo "4) Windows"
echo "5) Other"
echo "6) Exit"
echo -n "Xin chọn 1 mục (1..5) :"
read chon
case $chon in
1) echo "You have selected UNIX";;
2) echo "You have selected Solaris";;
3) echo "You have selected Free BSD";;
4) echo "You have selected Windows";;
5) echo "You have selected Other";;
6) exit 1;;
*) echo "Error";;
3


esac
}
while [ "$chon" != "6" ]
do
menu
done
#!/bin/bash
kq=0
while [ "$kq" != "6" ]
do
clear
echo "1 ) UNIX"
echo "2) Solaris"
echo "3) Free BSD"
echo "4) Windows"
echo "5) Other"
echo "6) Exit"
echo -n "Xin chọn 1 mục (1..5) :"
read chon
case $chon in
1) echo "You have selected UNIX";;
2) echo "You have selected Solaris";;
3) echo "You have selected Free BSD";;
4) echo "You have selected Windows";;
5) echo "You have selected Other";;
6) exit 1;;
*) echo "Error";;
esac
echo -n "Bam enter de tiep tuc"
read
done
#!/bin/bash
case `date +'%A'` in
Monday) echo "Thu hai";;
Tuesday) echo "Thu ba";;
Wednesday) echo "Thu tu";;
Thurday) echo "Thu nam";;
Friday) echo "Thu sau";;
Saturday) echo "Thu bay";;
Sunday) echo "Chu nhat";;
*) echo "Error";;
esac
cuu chuong
#!/bin/bash
i=1
while [ $i -le 10 ]
4


do
echo $1x$i=`expr $i \* $1`
i=`expr $i + 1`
done
giai thua
giaithua $prev
f

result=`expr $result \* $1`

}
result=1
giaithua $1
echo "giai thua $1 = $result"
in sao
#!/bin/bash
for (( i = 1;i <= $1; i++ ))
do
for (( j = 1;j <= i; j++ ))
do
echo -n "*"
done
echo " "
done
#!/bin/bash
for (( i = 1;i <= $1; i++ ))
do
for (( j = 1;j <= i; j++ ))
do
echo -n "$i"
done
echo " "
done
#!/bin/bash
for (( i = 1;i <= $1; i++ ))
do
for (( j = 1;j <= i; j++ ))
do
echo -n "$j"
done
echo " "
done
tao tap tin
#!/bin/bash
#taotaptin 10 name
count=1
while [ $count -le $1 ]
do
touch $2$count
5


count=`expr $count + 1`
done
tao thu muc
#!/bin/bash
#taothumuc 10 name
count=1
while [ $count -le $1 ]
do
mkdir $2$count
count=`expr $count + 1`
done
trung binh cong
#!/bin/bash
n=$#
tong=0
while [ $1 ]
do
tong=`expr $tong \+ $1`
shif
done
echo "trung binh cong =`expr $tong \/ $n`"
echo $tong
echo $n
#!/bin/bash
n=$#
tong=0
for var
do
tong=`expr $tong + $var`
done
echo "trung binh = `expr $tong / $n`"
#!/bin/bash
# tao/xoa nhieu thu muc CT
#kiem tra tham so
if [ -z "$1" ]
then
echo "Sai tham so"
echo "Cu phap test11 -t / -x"
f
if [ -z "$2" ]
then
echo "Chua nhap ten thu muc"
f
# kiem tra ton tai cua folder
if [ -d "$2" ]
then
6


if [ "$1" = "-x" ] #xoa thu muc
then
echo "dang xoa..."
sleep 1
rmdir $2
echo "xoa thanh cong"
f
elif [ "$1" = "-t" ] #tao thu muc
then
if [ -z "$3" ] #kiem tra tham so so luong
then
echo "Sai tham so"
echo "Cu phap test11 -t name soluong"
else
#tao thu muc
dem=1
while [ $dem -le $3 ]
do
mkdir $2$dem
dem=`expr $dem + 1`
done
echo "tao thanh cong thu muc"
f
else

echo "Thu muc khong ton tai"

f

#!/bin/bash
clear
t=`expr $# % 2`
q=0
if [ $# -ge 2 ] && [ $t -eq 0 ]
then
while [ $1 ]
do
echo "------------------------------------------"
if [ -f $1 ]
then
a=$1
shif
else
echo "$1 khong phai fle"
b=$1
q=1
shif
f
7


if [ -d $1 ] && [ $q -ne 1 ]
then
cp $a $1
echo "Da copy { $a } vao { $1 }"
shif
else
echo "$1 khong phai la thu muc hoac fle truoc do nhap sai"
echo "khong copy duoc"
shif
f
done
else

echo "tham so khong dung ==> taptin thumuc_dich"

f
if [ -d $1 ]
#b=0
then
a=`ls $1`
cd $1
for x in $a
do
if [ -f $x ]
then
echo $x
#b="$b $x"
f
done
#echo "$b"
#cd ..
else

echo "Ban nhap khong dung thu muc"
f
65656562312312 new
#!/bin/bash
#copy fle vao nhieu thu muc theo cu phap
#
# kiem tra toi thieu 2 tham so
if [ $# -lt 2 ]
then
echo "sai cu phap"
echo " "
exit 1
f
luu=""
8


while [ "$2" ]
do
luu="$luu $1"
shif
done
for thumuc in $luu
do
if [ ! -d $thumuc ]
then
read -p "Thu muc $thumuc chua co, tao? C/K?" bien
case $bien in
[cC]) mkdir $thumuc;;
*) exit 2
esac
elif [ -f $1 ]
then
cp $1 $thumuc
else
echo "$1 phai tap tin"
exit 3
f
done

9



Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Tải bản đầy đủ ngay

×