043 while loops kho tài liệu training

21 35 0
043 while loops kho tài liệu training

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

While Loops LinuxTrainingAcademy.com What You Will Learn ● ● ● While loops Infinite loops Loop control ○ ○ ○ ● ● Explicit number of times User input Command exit status Reading files, line-by-line break and continue LinuxTrainingAcademy.com While Loop Format while [ CONDITION_IS_TRUE ] command command command N done LinuxTrainingAcademy.com While Loop Format while [ CONDITION_IS_TRUE ] # Commands change the condition command command command N done LinuxTrainingAcademy.com Infinite Loops while [ CONDITION_IS_TRUE ] # Commands NOT change # the condition command N done LinuxTrainingAcademy.com Infinite Loops while [ true ] command N sleep done LinuxTrainingAcademy.com Example - Loop Times INDEX=1 while [ $INDEX -lt ] echo "Creating project-${INDEX}" mkdir /usr/local/project-${INDEX} ((INDEX++)) done LinuxTrainingAcademy.com Output - Loop Times Creating Creating Creating Creating Creating project-1 project-2 project-3 project-4 project-5 LinuxTrainingAcademy.com Example - Checking User Input while [ "$CORRECT" != "y" ] read -p "Enter your name: " NAME read -p "Is ${NAME} correct? " CORRECT done LinuxTrainingAcademy.com Output - Checking User Input Enter your name: Luke Skywalker Is Luke Skywalker correct? n Enter your name: Jason Is Jason correct? y LinuxTrainingAcademy.com Example - Return Code of Command while ping -c app1 >/dev/null echo "app1 still up " sleep done echo "app1 down, continuing." LinuxTrainingAcademy.com Output - Return Code of Command app1 app1 app1 app1 app1 app1 still still still still still down, up up up up up continuing LinuxTrainingAcademy.com Reading a file, line-by-line LINE_NUM=1 while read LINE echo "${LINE_NUM}: ${LINE}" ((LINE_NUM++)) done < /etc/fstab LinuxTrainingAcademy.com Output - Reading a file, line-by-line 1: 2: 3: 4: 5: # /etc/fstab # /dev/mapper/centos-root / xfs LABEL=boot /boot xfs /dev/mapper/centos-swap swap swap defaults defaults defaults 1 0 LinuxTrainingAcademy.com Reading a file, line-by-line grep xfs /etc/fstab | while read LINE echo "xfs: ${LINE}" done LinuxTrainingAcademy.com Output - Reading a file, line-by-line xfs: /dev/mapper/centos-root / xfs: LABEL=boot /boot xfs xfs defaults defaults 1 LinuxTrainingAcademy.com FS_NUM=1 grep xfs /etc/fstab | while read FS MP REST echo "${FS_NUM}: file system: ${FS}" echo "${FS_NUM}: mount point: ${MP}" ((FS_NUM++)) done LinuxTrainingAcademy.com 1: 1: 2: 2: file system: mount point: file system: mount point: /dev/mapper/centos-root / LABEL=boot /boot LinuxTrainingAcademy.com while [ true ] read -p "1: Show disk usage 2: Show uptime " CHOICE case "$CHOICE" in 1) df -h ;; 2) uptime ;; *) break ;; esac done LinuxTrainingAcademy.com mysql -BNe 'show databases' | while read DB db-backed-up-recently $DB if [ "$?" -eq "0" ] then continue fi backup $DB done LinuxTrainingAcademy.com Summary ● ● ● While loops Infinite loops Loop control ○ ○ ○ ● ● Explicit number of times User input Command exit status Reading files, line-by-line break and continue LinuxTrainingAcademy.com ... command N done LinuxTrainingAcademy.com Infinite Loops while [ CONDITION_IS_TRUE ] # Commands NOT change # the condition command N done LinuxTrainingAcademy.com Infinite Loops while [ true ] command... ● ● ● While loops Infinite loops Loop control ○ ○ ○ ● ● Explicit number of times User input Command exit status Reading files, line-by-line break and continue LinuxTrainingAcademy.com While Loop... 'show databases' | while read DB db-backed-up-recently $DB if [ "$?" -eq "0" ] then continue fi backup $DB done LinuxTrainingAcademy.com Summary ● ● ● While loops Infinite loops Loop control

Ngày đăng: 17/11/2019, 08:18

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan