Requirement 1:Use the for loop to create ten files in the/root/scripts/practice/q1/oldboy directory. The names are as follows:
Oldboy-1,
Oldboy-2,
.....
Oldboy-10.
Script implementation:
#! /Bin/bash # question1shopt-s expand_aliasesalias LL = "ls-L" CD/root/scripts/practice/q1 # first, determine whether the directory exists. If it does not exist, create the directory if [! -E oldboy] Then/bin/mkdir oldboyelse echo "" fipath =/root/scripts/practice/q1/oldboycd $ pathfor (I = 1; I <= 10; I ++) Do/bin/touch oldboy-$ idonell $ path
Script Execution result:
# [Email protected] Q1] # PWD/root/scripts/practice/q1 [[email protected] Q1] # ll total 4-rw-r -- r -- 1 Root 286 06 -26 q1.sh [[email protected] Q1] # [[email protected] Q1] # sh q1.sh total 0-rw-r -- r -- 1 Root 0 06-26 oldboy-1-rw-r -- r -- 1 root 0 06-26 oldboy-10-rw-r -- r -- 1 Root 0 06-26 oldboy-2-rw-r -- r -- 1 Root 0 06-26 oldboy-3-rw-r -- r -- 1 Root 0 06 -26 oldboy-4-rw-r -- r -- 1 Root 0 06-26 oldboy-5-rw-r -- r -- 1 Root 0 06-26 oldboy-6-rw-r -- r -- 1 Root 0 06-26 oldboy-7-rw-r -- r -- 1 Root 0 06-26 oldboy-8-rw-r -- r -- 1 Root 0 06-26 oldboy-9 # When the directory exists # Clear the oldboy directory, but do not delete this directory, the simulated directory contains [[email protected] Q1] # rm-RF oldboy/* [[email protected] Q1] # lsoldboy q1.sh [[email protected] Q1] # ll oldboy/total 0 [[email protected] Q1] # sh q1.sh total 0-rw-r -- r -- 1 Root 0 06-26 oldboy-1-rw-r -- r -- 1 Root 0 06-26 oldboy-10-rw-r -- r -- 1 root 0 06-26 oldboy-2-rw-r -- r -- 1 Root 0 06-26 oldboy-3-rw-r -- r -- 1 Root 0 06-26 oldboy-4-rw-r -- r -- 1 Root 0 06 -26 oldboy-5-rw-r -- r -- 1 Root 0 06-26 oldboy-6-rw-r -- r -- 1 Root 0 06-26 oldboy-7-rw-r -- r -- 1 Root 0 06-26 oldboy-8-rw-r -- r -- 1 Root 0 06-26 oldboy-9 [[[email protected] Q1] # ll oldboy/total 0-rw-r -- r -- 1 Root 0 06-26 oldboy-1-rw-r -- r -- 1 Root 0 06-26 oldboy-10-rw-r -- r -- 1 Root 0 06-26 oldboy-2-rw-r -- r -- 1 Root 0 06-26 oldboy-3-rw-r -- r -- 1 Root 0 06-26 oldboy-4-rw-r -- r -- 1 Root 0 06-26 oldboy-5-rw-r -- r -- 1 Root 0 06-26 oldboy-6-rw-r -- r -- 1 Root 0 06-26 -- r -- 1 Root 0 06-26 oldboy-7-rw-r -- r -- 1 Root 0 06-26 oldboy-8-rw-r -- r -- 1 Root 0 06-26 oldboy-9
The script can normally complete the file creation task.
Requirement 2: modify all oldboy-files created by appeal in batches using the for loop to Linux -.
Script implementation:
#!/bin/bash#Question2shopt -s expand_aliasesalias ll=‘ls -l‘path=/root/scripts/practice/q1/oldboycd $pathfor ((i=1;i<=10;i++))do /bin/mv oldboy-$i Linux-$idonell $path
Execution result:
[[Email protected] Q1] # sh q2.sh total 0-rw-r -- r -- 1 Root 0 06-26 Linux-1-rw-r -- r -- 1 Root 0 06-26 Linux-10-rw-r -- r -- 1 root 0 06-26 Linux-2-rw-r -- r -- 1 Root 0 06-26 Linux-3-rw-r -- r -- 1 Root 0 06-26 Linux-4-rw-r -- r -- 1 Root 0 06 -26 Linux-5-rw-r -- r -- 1 Root 0 06-26 Linux-6-rw-r -- r -- 1 Root 0 06-26 Linux-7-rw-r -- r -- 1 Root 0 06-26 Linux-8-rw-r -- r -- 1 Root 0 06-26 Linux-9 [[[email protected] Q1] # ls oldboy/Linux-1 Linux-2 Linux-4 Linux-6 Linux-8Linux-10 Linux-3 Linux-5 Linux-7 Linux-9 [[email protected] Q1] #
The name has been successfully renamed.
This article is from "Shi zhenning's technology blog" blog, please be sure to keep this source http://magic3.blog.51cto.com/1146917/1431155