The 32nd day of learning big data - cycle and date

The 32nd day of learning big data - cycle and date

for loop

Format 1:

for((i=1;i<=j;i++))

do

Circulatory body

done

Format 2:

for i in {start position... End position} # there are two points in the middle

do

Circulatory body

done

Format 3:

for i in $(seq end position)

do

Circulatory body

done

Format 4:

for i in $(seq 5 -1 1) start position plus or minus end position

do

Circulatory body

done

Note: the number of outputs is different

Format 2: output the number of 0 1 2 Subscripts

for i in (0..2)
do
	Circulatory body
done

Format 3: output the number of 0 and 1 Subscripts

for i in $(seq 2)
do
	Circulatory body
done

Circular exercise

Exercise 1:

Generate four random numbers

Format 1:
for((i=1;i<=4;i++))
do
	echo $RANDOM
done

Format 2:
for i in {0..4}
do
	echo $RANDOM
done

Format 3:
for i in $(seq 5)
do
	echo $RANDOM
done
Exercise 2:

Count down to five seconds

Method 1: prepare to count down five seconds (use format 4)
for i in $(seq 5 -1 1)
do
	echo -en "$i"
	sleep 1
done

Method 2: (use format 1)
for((i=1;i<=5;i++))
do
	echo -en "$i"
	sleep 1
done
Exercise 3: export in format
# Output in format
#  $1 is aa,
#  $2 is bb,
#  $3 is cc,
#  $4 is dd,
#  $5 is ee


N=1 
for i in $@
do
        echo "\$$N is $i"
        ((N++))
done
Exercise 4: 99 multiplication table
#multiplication table
for((i=1;i<=9;i++))
do
        for((j=1;j<=i;j++))
        do
                echo -en "$j*$i="$(( i + j ))"\t"
        done
        echo -e "\n"
done

function

Start from three aspects: 1. No return value without parameters, 2. Return value without parameters, 3. Return value without parameters

Format:

Function name (){

Method body

​ [return int;]

}

explain:

  • 1. It can be defined with function fun() or directly without any parameters.
  • 2. Parameter return, plus can be displayed: return returns. If not, the result of the last command will be used as the return value. Return followed by the value n(0-255)
Function formats with no parameters and no return values and functions without parameters but with return values
#!/bin/sh

#Considerations for functions
#Format function name (){
#       Method body
#     }

#Function format with no parameters and no return value
demoFun(){
        echo "This is my first shell function"
}
echo "-----Function starts execution-----"
demoFun
echo "-----Function execution completed-----"

#Function format with no parameters but return value
# $?  If there is a return value in the range of 0-255, the result will be output
#    If the range of the return value is not between the ranges, an individual value will be output
#    If the input is a negative number, it still depends on whether the result is in this range. If it is, the result will be output. If it is not, it is a negative number, and it will be > out of range
#   Keyboard input: read
funWithReturn(){
        echo "This function adds the two input numbers..."
        echo "Enter the first number:"
        read num1
        echo "Enter the second number:"
        read num2
        echo "The two numbers are $num1 and $num2 "
        return $(($num1+$num2))
#       return 300
}
funWithReturn
echo "The sum of the two numbers entered is $? !"

No return value but parameter
#No return value but parameter
#If the output value exceeds the parameter range, no error will be reported, but it will not be output
fun(){
        echo "The first parameter is $1 !" 
        echo "The second parameter is $2 !"
        echo "The third parameter is $3 !"
        echo "The fifth parameter is $5 !" 
        echo "The tenth parameter is ${12} !"
        echo "The total number of parameters is $# One“
        echo "Output all parameters as a string $* !"
}
fun 1 2 3 4 5 6 7 8 9 10

date

#!/bin/sh

#shell date

#Get current date and time
date

#Displays the date and time in the specified format
date "+%Y-%m-%d %H:%M:%S"

#Set system date
#date -s "2017-01-01 01:01"
#date -set="2017-01-01 01:01"

#Sometimes, when we operate the date and time, we often need to obtain the time of the first few days or the next few days. The date command also provides us with options' - d 'and' - date 'to realize this function

#Date can be used to display or set the date and time of the system. In terms of display, the user can set the format to be displayed. The format is set as a plus sign followed by several marks. The list of available marks is as follows:

#Get the time of the next day
date -d next-day "+%Y-%m-%d %H:%M:%S"

#Get the time of the next day (the second way)
date -d tomorrow '+%Y-%m-%d %H:%M:%S'

#Get the time of the day
date -d last-day '+%Y-%m-%d %H:%M:%S'

#Get the time of the previous day (the second way)
date -d yesterday '+%Y-%m-%d %H:%M:%S'

#Get the time of next month
date -d next-month '+%Y-%m-%d %H:%M:%S'

#Get the time of last month
date -d last-month '+%Y-%m-%d %H:%M:%S'

#Get the time of the next year
date -d next-year '+%Y-%m-%d %H:%M:%S'

#Get the time of the previous year
date -d last-year '+%Y-%m-%d %H:%M:%S'

#Get the date and time of the next week
date -d next-week '+%Y-%m-%d %H:%M:%S'
date -d next-monday '+%Y-%m-%d %H:%M:%S'
date -d next-thursday '+%Y-%m-%d %H:%M:%S'

#Sunday, March 6, 2022 21:41:27
date '+%c'
date '+%D'
date '+%U'

Date aspect

%a: day of the week (Sun... Sat)

%A: Sunday... Saturday

%b: month (Jan... Dec)

%B: January... December

%c: direct display of date and time

%d: Day (01... 31)

%D: direct display date (mm/dd/yy)

%h: same as% b

%j: the day of the year (001... 366)

%m: month (01... 12)

%U: the week of the year (00... 53) (with Sunday as the first day of the week)

%w: the day of the week (0... 6)

%W: the week of the year (00... 53) (taking Monday as the first day of the week)

%x: direct display date (mm/dd/yyyy)

%y: the last two digits of the year (00.99)

%Y: full year (0000... 9999)

Time aspect

%%: print out%

%n: next line

%t: skip

%H: hours (00... 23)

%k: hours (0... 23)

%l: hours (1... 12)

%M: minutes (00... 59)

%p: display local AM or PM

%P: display local am or pm

%r: direct display time (12 hour system, format hh:mm:ss [AP]M)

%s: the number of seconds since 00:00:00 UTC on January 1, 1970

%S: seconds (00... 61)

%T: direct display time (24-hour system)

%X: equivalent to% H:% m:% s% p

%Z: display time zone

There is also a way of adding and subtracting time

#Current time
date +"%Y-%m-%d %H:%M:%S"

#Time of the next day (tomorrow)
date -d "+1 day" +"%Y-%m-%d %H:%M:%S"

#Time of the previous day (yesterday)
date -d "-1 day" +"%Y-%m-%d %H:%M:%S"

#Time of the next month (next month)
date -d "+1 month" +"%Y-%m-%d %H:%M:%S"

#Time of the previous month (last month)
date -d "-1 month" +"%Y-%m-%d %H:%M:%S"

#The next week
date -d "+1 week" +"%Y-%m-%d %H:%M:%S"

#Previous time
date -d "-1 week" +"%Y-%m-%d %H:%M:%S"

#Time of next year (next year)
date -d "+1 year" +"%Y-%m-%d %H:%M:%S"

#Time of the previous year (last year)
date -d "-1 year" +"%Y-%m-%d %H:%M:%S"

Key points: useful tips for dates

Get the date before or after a date:

[root@hadoop ~]# date -d 'may 14 -2 weeks'

Remove the useless zeros in time. For example, 01:02:25 will become 1:2:25

[root@hadoop ~]# date '+%-H:%-M:%-S'

Displays the time when the file was last changed

[root@hadoop ~]# date "+%Y-%m-%d %H:%M:%S" -r bin/removeJDK.sh

Find the number of days between two string dates

[root@hadoop ~]#

expr '(' $(date +%s -d "2016-08-08") - $(date +%s -d "2016-09-09") ')' / 86400

expr expr $(date +%s -d "2016-08-08") - $(date +%s -d "2016-09-09") / 86400

r

Add or subtract the specified interval unit in the shell

[root@hadoop ~]# A=date +%Y-%m-%d

[root@hadoop ~]# B=date +%Y-%m-%d -d "$A +48 hours"

Continue to review the important knowledge points - redirection and timer tomorrow

Keywords: Linux Big Data

Added by fatfrank on Sun, 06 Mar 2022 16:39:34 +0200