From 2055189b70e8b72500d43c0f0232b4d41a1b0a7e Mon Sep 17 00:00:00 2001 From: Wu Mianzhi <10215501411@stu.ecnu.edu.cn> Date: Mon, 12 Sep 2022 11:06:24 +0800 Subject: [PATCH] Add Homework Lab1 --- lab1/10215501411.sh | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 lab1/10215501411.sh diff --git a/lab1/10215501411.sh b/lab1/10215501411.sh new file mode 100644 index 0000000..c78f8f0 --- /dev/null +++ b/lab1/10215501411.sh @@ -0,0 +1,57 @@ +SAMESTR="ThisIsTheSameString" #Same String for uniq +readonly SAMESTR + +## +# Get random integar between 1 and 100 +## +function getRandomName(){ + rnd=`expr $RANDOM % 64 + 1` + return $rnd +} + +## +# Get random string, first parametar is length +## +function getRandomString(){ + openssl rand -base64 $1 +} + +function generate(){ + size=500 + i=0 + while(( $i < $size )) + do + getRandomName + length=$? + #echo "START" + if [[ $length -ge 1 && $length -le 25 ]] + then + echo $SAMESTR + else + echo `getRandomString $length` + fi + let "i++" + done +} + +function sortByASCII(){ + generate | sort | uniq -u +} + +function sortByNumeric(){ + generate | sort --numeric-sort | uniq -u +} + +echo "Overwrite lines:" > resultASCII #this line should not appear in output file +sortByASCII > resultASCII +echo "Append lines:" >> resultASCII #this line should appear in output file +sortByASCII >> resultASCII + +echo "Overwrite lines:" > resultNumeric #this line should not appear in output file +sortByNumeric > resultNumeric +echo "Append lines:" >> resultNumeric #this line should appear in output file +sortByNumeric >> resultNumeric + +echo "Number of same string in file:" +generate > randomLines +grep --only-matching $SAMESTR < randomLines | wc --lines \ No newline at end of file