1. 程式人生 > 實用技巧 >Shell--常用變數

Shell--常用變數

1、判斷是不是月底最後一天?
#!/bin/bash
((`date '+%d'` == 1 || `date '+%d' -d tomorrow` == 1))
echo $?
2.生成隨機字元
#!/bin/bash
function Rand_Digit_Pass(){
    digit_pass_len=5 #定義密碼長度
    digit_seq=(0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9) #數字範圍
    i=1
    digit_len=${#digit_seq[@]} #陣列變數長度
    ###生成數字密碼
    while [ "
$i" -le "$digit_pass_len" ] do digit_rand[$i]=${digit_seq[$((RANDOM%digit_len))]} #存入陣列digit_rand中 let "i=i+1" done for digit_pass in ${digit_rand[@]} do echo -n "$digit_pass" done } function Rand_Str_Pass(){ str_pass_len=3 str_seq=(a b c d e f g h i j k l m n o p q r s t u v w
x y z A B C D E F G H I J K L M N O P Q R S T U V W X Y Z) j=1 str_len=${#str_seq[@]} ###生成字元密碼 while [ "$j" -le "$str_pass_len" ] do str_rand[$j]=${str_seq[$((RANDOM%str_len))]} let "j=j+1" done for str_pass in ${str_rand[@]} do echo -n "$str_pass
" done } RandPass=$(Rand_Digit_Pass)$(Rand_Str_Pass) echo $RandPass