shell编程基础之根据输入进行相应的操作
生活随笔
收集整理的這篇文章主要介紹了
shell编程基础之根据输入进行相应的操作
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
這個題目是
輸入A:創建自己名字的文件夾,如果有這個文件夾的話,就讀取file1和file2的文件內容
輸入B:獲取當前用戶對file1和file2文件權限
輸入C:內容轉為大寫
輸入D:計算連接和文件數目
輸入E:計算文件空行數目
以下是我的代碼:
#! bin/bash read opts; case $opts in 'A') if [ -d ~/li ];thencp ./file1 ~/li;cp ./file2 ~/li;cat ~/li/file1;cat ~/li/file2; elsemkdir ~/li;chmod -R 755 ~/li;fi;; 'B') if [ -d ~/liwei ];thenfile1=$(ls -l ~/li | grep 'file1'| awk -F " " '{print $1}' | cut -d '.' -f 1);file2=$(ls -l ~/li | grep 'file2'| awk -F " " '{print $1}' | cut -d '.' -f 1);echo your power of file1 is $file1;echo your power of file2 is $file2;elseecho please excute 'A' first;fi;; 'C') if [ -d ~/li];thenfile1=$(cat ~/li/file1);file2=$(cat ~/li/file2);upper=$(echo $file1 | tr '[a-z]' '[A-Z]');lower=$(echo $file2 | tr '[a-z]' '[A-Z]');echo 'uppercase show of file1 and file2:';echo $upper;echo $lower;elseecho please excute 'A' first;fi ;; 'D') file_str=$(ls -l /dev | awk -F " " '{print $1}' | cut -c 1 | tr \n , );file_arr=($file_str);l_num=0;d_num=0;for x in ${file_arr[@]}do if [ "$x" = "d" ];thend_num=$(($d_num + 1));fiif [ "$x" = "l" ];thenl_num=$(($l_num + 1));fidoneecho the number of directory in dev is $d_num;echo the number of link in dev is $l_num;;; 'E') line_num=0;test_num=0;if [ -d ~/li ];thenwhile read linedoif [[ ! -n $line ]];thenline_num=$(($line_num + 1));fidone < ~/li/file1echo "the number of blank line is $line_num"; elseecho please excute 'A' first.fi;; esac exit?
轉載于:https://www.cnblogs.com/rimochiko/p/8168620.html
總結
以上是生活随笔為你收集整理的shell编程基础之根据输入进行相应的操作的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 2017年总结的前端文章——CSS高级技
- 下一篇: 管理信息系统 第三部分