下面是我在工作中编写的shelldemo,里面包含了常用方法,包括当前事件的获得和格式化,字符串的分割,文件和目录的判断,字符串前缀的对比等等,我还会继续扩展这个demo,希望对大家有用:)
currenttime=`date '+%Y-%m-%d %H:%M:%S'` var=`date` echo `date` echo $var #得到当前事件 echo "$currenttime" #!/bin/bash echo `pwd` #字符串的分割 # var=`echo "aaa,bbb,ccc"|awk -F ',' '{print $1}' ` # echo $var #遍历“config”目录 for config in `ls config`; do echo "$config" #字符串根据“#”来分隔,print打印,计数器从0开始 # var=`echo "$config"|awk -F '#' '{print $1}' ` # echo $var #! [[ ...,这里的求反必须有空格,否则报错 # no_*为前缀,不可以加引号 if ! [[ "$config" = _* ]]; then echo "----------" continue; fi for con in `ls config/$config`; do echo "config/$config/$con" # if [[ -d $con ]]; then #这里必须是"config/$config/$con",否则会出现文件能识别,但文件夹不能识别的错误 #递归拷贝所有的config文件到主目录中 # --判断con是文件和还是目录-- if [[ -d "config/$config/$con" ]]; then echo "$con is a directory ..." cp -r "config/$config/$con" . elif [[ -f "config/$config/$con" ]]; then echo "$con is a file ..." cp "config/$config/$con" . else echo "$con is not valid ..." exit 1 fi done done
输出结果如下:
victormatoimac:shelltest ericyang$ sh ./autotest.sh 2013年 1月 8日 星期二 16时35分51秒 CST 2013年 1月 8日 星期二 16时35分51秒 CST 2013-01-08 16:35:51 /Users/ericyang/shelltest _con1 config/_con1/a.txt a.txt is a file ... config/_con1/c1 c1 is a directory ... con2 ----------
Description
Farmer John wants to repair a small length of the fence around the pasture. He measures the fence and finds that he needs N (1 ≤ N ≤ 20,000) planks of wood, each having some integer length Li (1 ≤ Li ≤ 50,000) units. He then purchases a single long board just long enough to saw into the N planks (i.e., whose length is the sum of the lengths Li). FJ is ignoring the "kerf", the extra length lost to sawdust when a sawcut is made; you should ignore it, too.
FJ sadly realizes that he doesn't own a saw with which to cut the wood, so he mosies over to Farmer Don's Farm with this long board and politely asks if he may borrow a saw.
Farmer Don, a closet capitalist, doesn't lend FJ a saw but instead offers to charge Farmer John for each of the N-1 cuts in the plank. The charge to cut a piece of wood is exactly equal to its length. Cutting a plank of length 21 costs 21 cents.
Farmer Don then lets Farmer John decide the order and locations to cut the plank. Help Farmer John determine the minimum amount of money he can spend to create the N planks. FJ knows that he can cut the board in various different orders which will result in different charges since the resulting intermediate planks are of different lengths.
Input
Lines 2..N+1: Each line contains a single integer describing the length of a needed plank
Output
Sample Input
3 8 5 8
Sample Output
34
Hint
The original board measures 8+5+8=21. The first cut will cost 21, and should be used to cut the board into pieces measuring 13 and 8. The second cut will cost 13, and should be used to cut the 13 into 8 and 5. This would cost 21+13=34. If the 21 was cut into 16 and 5 instead, the second cut would cost 16 for a total of 37 (which is more than 34).
Source
#include <iostream> #include <cstring> #include <stdlib.h> #include <stdio.h> using namespace std; int a[20010]; int cmp(const void *e,const void *f) { return (*(int *)e-*(int *)f); } int main() { int i,j,n,m,t; __int64 sum,s; scanf("%d",&n); for(i=0;i<=n-1;i++) { scanf("%d",&a[i]); } qsort(a,n,sizeof(a[0]),cmp); sum=0; for(i=1;i<=n-1;i++) { s=(a[i-1]+a[i]); sum+=s; for(j=i+1;j<=n-1;j++) { if(a[j]<s) { a[j-1]=a[j]; }else { break; } } a[j-1]=s; } printf("%I64d\n",sum); return 0; }
关于移动文件或者文件夹的话可以选择的有三个函数:Directory.Move();、File.Move();和DirectoryInfo.MoveTo();
其实要是一般情况下,这三个函数都可以满足要求。
然而可是,这些个函数都有很多异常。
最明显的一条就是,前面两个函数移动的文件不可以移动到磁盘的根目录下面。要想移动到磁盘根目录下面就要用到最后一个函数。
格式:
DirectoryInfo infoReplace = new DirectoryInfo(string SourceName);
infoReplace.MoveTo(string TargetName); //TargetName在这里是磁盘根目录,比如C:\
其实移动文件或者文件夹而不是拷贝文件或者文件夹,
这里是有区别的,移动后原来的文件或者文件夹就没有了,可是要是拷贝的话,原来的文件或者文件夹还会在。