关于shell-script:shell脚本-逐个压缩文件夹内所有文件和文件夹

Linux下通过zip命令一一压缩某文件夹下的子文件夹和文件(反对文件名中带空格场景): filename : zipfolder #!/bin/bashIFS=$'\n'for file in ` ls -1 $1 `do if [[ ${file} == *".zip" ]] then echo " skipped: ${file}" else zip -r "$1/${file}.zip" "$1/${file}" fidone示例: 执行前: MyMacBook-Pro:~ root$ find /Users/root/targetfolder -print | sed -e 's;1*/;|____;g;s;____|; |;g'| | |____targetfolder| | | |____file1.txt| | | |____folder1| | | | |____2.txt| | | |____zipfile.zip执行: MyMacBook-Pro:~ root$ ./zipfolder /Users/root/targetfolder adding: /Users/root/targetfolder/file1.txt (deflated 33%) adding: /Users/root/targetfolder/folder1/ (stored 0%) adding: /Users/root/targetfolder/folder1/2.txt (deflated 33%)Skipped: zipfile.zip执行后: ...

May 4, 2021 · 1 min · jiezi

关于shell-script:shell-ifelsefi

文件表达式 -e filename 如果 filename存在,则为真-d filename 如果 filename为目录,则为真 -f filename 如果 filename为惯例文件,则为真-L filename 如果 filename为符号链接,则为真-r filename 如果 filename可读,则为真 -w filename 如果 filename可写,则为真 -x filename 如果 filename可执行,则为真-s filename 如果文件长度不为0,则为真-h filename 如果文件是软链接,则为真filename1 -nt filename2 如果 filename1比 filename2新,则为真。filename1 -ot filename2 如果 filename1比 filename2旧,则为真。整数变量表达式 -eq 等于-ne 不等于-gt 大于-ge 大于等于-lt 小于-le 小于等于字符串变量表达式 If [ $a = $b ] 如果string1等于string2,则为真 字符串容许应用赋值号做等号if [ $string1 != $string2 ] 如果string1不等于string2,则为真 if [ -n $string ] 如果string 非空(非0),返回0(true) if [ -z $string ] 如果string 为空,则为真if [ $sting ] 如果string 非空,返回0 (和-n相似) 逻辑非 ! ,条件表达式的相同 ...

February 4, 2021 · 2 min · jiezi

Linux运维shell脚本4

这篇主要记录下数组的使用,相比来说用得比较少,同样的根据阅读<<跟老男孩学Linux运维:Shell编程实战>>所记录系列笔记传送门: Linux运维:shell脚本(1)Linux运维:shell脚本(2)Linux运维:shell脚本(3)Linux运维:shell脚本(4)shell数组Shell数组是一个元素的集合,它把有限个元素用一个名字来命名,然后用编号对所有元素进行区分。这个名字就是数组名,用于区分不同内容的编号就叫数组的下标,组成数组的各个元素就是数组的元素,也叫下标变量。 1、数组的常用操作1.1 数组的定义 数组的定义有多种方法: 1、用小括号将变量值括起来赋值给变量,每个变量值之间用空格隔开。2、用小括号将变量值括起来,同时采用键值对的形式赋值。3、通过分别定义数组变量的方法定义。4、动态定义数组变量,并使用命令的输出结果作为数组内容。方法1示例: [root@moli_linux1 ~]# array=(one two three)[root@moli_linux1 ~]# echo ${array[*]}one two three[root@moli_linux1 ~]# echo ${array[0]}one[root@moli_linux1 ~]# echo ${array[1]}two[root@moli_linux1 ~]# echo ${array[2]}three方法2示例: [root@moli_linux1 ~]# array=([1]=one [2]=two [3]=three)[root@moli_linux1 ~]# echo ${array[*]}one two three[root@moli_linux1 ~]# echo ${array[1]}one[root@moli_linux1 ~]# echo ${array[2]}two[root@moli_linux1 ~]# echo ${array[3]}three方法3示例: [root@moli_linux1 ~]# array[0]=a;array[1]=b;array[2]=c[root@moli_linux1 ~]# echo ${array[*]}a b c[root@moli_linux1 ~]# 方法4示例: [root@moli_linux1 test]# touch {1..3}.txt[root@moli_linux1 test]# ls1.txt 2.txt 3.txt[root@moli_linux1 test]# array=($(ls .))[root@moli_linux1 test]# echo ${array[*]}1.txt 2.txt 3.txt[root@moli_linux1 test]# 1.2 数组打印输出 ...

July 10, 2019 · 2 min · jiezi

Linux运维shell脚本3

总结一下碰到过的shell脚本。1、统计进程占用内存大小写一个脚本计算一下linux系统所有进程占用内存大小的和。 #!/bin/bashsum=0for mem in `ps aux | awk '{print $6}' | grep -v 'RSS'`do sum=$[$sum + $mem]doneecho "The total memory is $sum"2、批量创建用户写一个脚本实现:添加user_00-user-09总共10个用户,并且对这10个用户设置一个随机密码,密码要求10位包含大小写字母以及数字,最后将每个用户的密码记录到一个日志文件里。 #!/bin/bashfor i in `seq -w 0 09`do useradd user_$i p=`mkpasswd -s 0 -l 10` echo "user_$i $p" >> /tmp/user0_9.pass echo $p |passwd --stdin user_$idone3、URL检测脚本编写shell脚本检测URL是否正常。 #!/bin/bash. /etc/init.d/functionsfunction usage(){ echo $"usage:$0 url" exit 1}function check_url(){ wget --spider -q -o /dev/null --tries=1 -T 5 $1 if [ $? -eq 0 ];then action "$1 is yes." /bin/true else action "$1 is no." /bin/false fi}function main(){ if [ $# -ne 1 ];then usage fi check_url $1}main $*测试结果: ...

July 1, 2019 · 1 min · jiezi

shell

1.datedate后边可以接两个参数,一个是[选项],一个是[格式],都是选填。[选项]为空,则为当前时间;[格式]为空,则为默认格式。如下:$ dateSat Jan 12 22:12:50 CST 2019通过date –h得到帮助如下:]$ date –hUsage: date [OPTION]… [+FORMAT] or: date [-u|–utc|–universal] [MMDDhhmm[[CC]YY][.ss]]Display the current time in the given FORMAT, or set the system date. -d, –date=STRING display time described by STRING, not now' -f, --file=DATEFILE like --date once for each line of DATEFILE -r, --reference=FILE display the last modification time of FILE -R, --rfc-2822 output date and time in RFC 2822 format. Example: Mon, 07 Aug 2006 12:34:56 -0600 --rfc-3339=TIMESPEC output date and time in RFC 3339 format. TIMESPEC=date’, seconds', or ns’ for date and time to the indicated precision. Date and time components are separated by a single space: 2006-08-07 12:34:56-06:00 -s, –set=STRING set time described by STRING -u, –utc, –universal print or set Coordinated Universal Time –help display this help and exit –version output version information and exitFORMAT controls the output. Interpreted sequences are: %% a literal % %a locale’s abbreviated weekday name (e.g., Sun) %A locale’s full weekday name (e.g., Sunday) %b locale’s abbreviated month name (e.g., Jan) %B locale’s full month name (e.g., January) %c locale’s date and time (e.g., Thu Mar 3 23:05:25 2005) %C century; like %Y, except omit last two digits (e.g., 20) %d day of month (e.g, 01) %D date; same as %m/%d/%y %e day of month, space padded; same as %_d %F full date; same as %Y-%m-%d %g last two digits of year of ISO week number (see %G) %G year of ISO week number (see %V); normally useful only with %V %h same as %b %H hour (00..23) %I hour (01..12) %j day of year (001..366) %k hour ( 0..23) %l hour ( 1..12) %m month (01..12) %M minute (00..59) %n a newline %N nanoseconds (000000000..999999999) %p locale’s equivalent of either AM or PM; blank if not known %P like %p, but lower case %r locale’s 12-hour clock time (e.g., 11:11:04 PM) %R 24-hour hour and minute; same as %H:%M %s seconds since 1970-01-01 00:00:00 UTC %S second (00..60) %t a tab %T time; same as %H:%M:%S %u day of week (1..7); 1 is Monday %U week number of year, with Sunday as first day of week (00..53) %V ISO week number, with Monday as first day of week (01..53) %w day of week (0..6); 0 is Sunday %W week number of year, with Monday as first day of week (00..53) %x locale’s date representation (e.g., 12/31/99) %X locale’s time representation (e.g., 23:13:48) %y last two digits of year (00..99) %Y year %z +hhmm numeric timezone (e.g., -0400) %:z +hh:mm numeric timezone (e.g., -04:00) %::z +hh:mm:ss numeric time zone (e.g., -04:00:00) %:::z numeric time zone with : to necessary precision (e.g., -04, +05:30) %Z alphabetic time zone abbreviation (e.g., EDT)By default, date pads numeric fields with zeroes.The following optional flags may follow `%’: - (hyphen) do not pad the field _ (underscore) pad with spaces 0 (zero) pad with zeros ^ use upper case if possible # use opposite case if possibleAfter any flags comes an optional field width, as a decimal number;then an optional modifier, which is eitherE to use the locale’s alternate representations if available, orO to use the locale’s alternate numeric symbols if available.Report date bugs to bug-coreutils@gnu.orgGNU coreutils home page: <http://www.gnu.org/software/coreutils/>General help using GNU software: <http://www.gnu.org/gethelp/>For complete documentation, run: info coreutils ‘date invocation’ ...

January 12, 2019 · 3 min · jiezi