#!/bin/bash
if [ -e $1 ]
then
chmod 600 $1
else
echo "The file $1 does not exist"
fi
#!/bin/bash
echo "Input a year number:"
read year
let "leap=$year%4==0&&$year%100!=0||$year%400==0"
if [ $leap -eq 0 ]
then
echo "$year is not a leap year"
else
echo "$year is a leap year"
fi
#!/bin/bash
for((i=0;i<$1;i++))
do
for((j=0;j<=i;j++))
do
echo -n "*"
done
echo ""
done
#!/bin/bash
s=0
for((i=0;i<=100;i++))
do
s=$(($s+$i));
done
echo "the sum is:$s"
#!/bin/bash
if [ $1 -gt 20 ]
then
echo "$(($1*$1))"
else
echo "number is wrong"
fi
#!/bin/bash
date
if who|grep "^$1"
then
echo "hello,$1!"
else
echo "waiting for $1!"
fi
#!/bin/bash
if [ -d $1 ]
then
ls -l $1
echo -n "d:"
ls -l $1|grep "^d"|wc -l
echo -n "-:"
ls -l $1|grep "^-"|wc -l
else
echo "$1 does not existence"
fi
#!/bin/bash
if [ -e $1 ]
then
if [ -e $2 ]
then
echo "replace?[yes/no]"
read ans
if [ $ans=="yes" ]
then
if [ -d $1 ]
then
cp -r $1 $2
else
cp $1 $2
fi
fi
else
if [ -d $1 ]
then
cp -r $1 $2
else
cp $1 $2
fi
fi
else
echo "error!"
fi
#!/bin/bash
case $1 in
-b) count = `grep ^b $2 | wc -l`
echo “The number of lines in $2 that start with b is $count.”;;
-s) count = `grep ^s $2 | wc -l`
echo “The number of lines in $2 that start with s is $count.”;;
*) echo “Theoption is not recognized”;;
esac
#!/bin/bash
if [[ -f $1 && -f $2 ]]
then
echo “please chose your cat, statistic, merge, copy, or bye.”
read chose
case $chose in
cat) cat $1 $2;;
statistic) echo `wc -l $1`
echo `wc -l $2`;;
merge) cat $1 >> $2;;
copy) cat $1 > $2;;
bye) break;;
esac
else
echo "$1 or $2 is not file"
fi
#!/bin/bash
for i in `ls|grep -E "*.c"`
do
mv $i $1
done
ls -l -r -S $1
#!/bin/bash
a=1
b=1
sum=2
cnt=4
echo -n -e "$a\t$b"
while [ $cnt -gt 0 ]
do
let "a=a+b"
let "b=a+b"
let "sum=sum+a+b"
let "cnt=cnt-1"
echo -n -e "\t$a\t$b"
done
echo ""
echo "sum=$sum"
#!/bin/bash
flag=0
if
for (( i=2; i<=$1/2;i++ ))
do
if (($1%i==0))
then
flag=1
break
fi
done
if [ flag -eq 1 ]
then
echo $1 is not a prime number
else
echo $1 is a prime number
fi
14)编写shell脚本,将给定的参数转换成二进制表示。
#!/bin/bash
a=0
b=$1
c=1
sum=0
while [ $b -gt 0 ]
do
let "a=b%2"
let "b=b/2"
let "sum=sum+a*c"
let "c=c*10"
done
echo "$sum"
#!/bin/bash
cd $1
for i in `cat studentlist.csv`
do
str="_homework.txt"
file=$i$str
if [ -f $file ]
then
echo "$i yes"
else
echo "$i no"
fi
done
有不同意见欢迎留言讨论!