wangsx@SC-201708020022:~$ cat test10 #!/bin/bash # redirecting all output to a file exec 1>testout
echo "This is a test of redirecting all output" echo "from a script to another file" echo "without having to redirect every individual line" wangsx@SC-201708020022:~$ sh test10 wangsx@SC-201708020022:~$ cat testout This is a test of redirecting all output from a script to another file without having to redirect every individual line
再结合STDERR看看
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
wangsx@SC-201708020022:~$ cat test11 #!/bin/bash # redirecting output to different locations
exec 2>testerror
echo "This is the start of the script" echo "now redirecting all output to another location"
exec 1>testout echo "This output should go to the testout file" echo "but this should go to the testerror file" >&2 wangsx@SC-201708020022:~$ sh test11 This is the start of the script now redirecting all output to another location wangsx@SC-201708020022:~$ cat testout This output should go to the testout file wangsx@SC-201708020022:~$ cat testerror but this should go to the testerror file
wangsx@SC-201708020022:~$ cat test14 #!/bin/bash # storing STDOUT, the coming back to it
exec 3>&1 exec 1>test14out
echo "This should store in the output file" echo "along with this line"
exec 1>&3 echo "Now things should be back to normal" wangsx@SC-201708020022:~$ sh test14 Now things should be back to normal wangsx@SC-201708020022:~$ cat test14out This should store in the output file along with this line
wangsx@SC-201708020022:~$ cat testfile This is the first line. This is the second line. This is the third line. wangsx@SC-201708020022:~$ cat /dev/null > testfile wangsx@SC-201708020022:~$ cat testfile wangsx@SC-201708020022:~$
wangsx@SC-201708020022:~/tmp$ cat test19 #!/bin/bash t nomore" \ # creating and using a temp file tempfile=$(mktemp test19.XXXXXX)
exec 3>$tempfile echo "This script writes to temp file $tempfile echo "This is the first line." >&3 echo "This is the last line." >&3 exec 3>&-
echo "Done creating temp file. The contents are:" cat $tempfile rm -f $tempfile 2> /dev/null wangsx@SC-201708020022:~/tmp$ sh test19 This script writes to temp file test19.fVVEwn Done creating temp file. The contents are: This is the first line. This is the last line.
#!/bin/bash #read file and create INSERT statements for MySQL
outfile='members.sql' IFS="," while read lname fname address city state zip # read 使用IFS字符解析读入的文本 do cat >> $outfile << EOF # >> 将cat的输出追加到$outfile指定的文件中 # cat的输入不再取自于标准输入,而是被重定向到脚本中存储的数据,EOF符号标记了追加到文件中的数据的起止(两个) INSERT INTO members (lname,fname,address,city,state,zip) VALUES ('$lname', '$fname', '$address', '$city', '$state', '$zip'); EOF # 上面是标准的SQL语句 done < $1 # 将命令行第一个参数指明的数据文件导入while
造一个符合的csv文件
1 2 3 4
wangsx@SC-201708020022:~/tmp$ cat members.csv Blum,Richard,123 Main St.,Chicago,IL,60601 Blum,Barbara,123 Main St.,Chicago,IL,60601 Bresnahan,Timothy,456, Oak Ave.,Columbus,OH,43201
运行脚本
1 2 3 4 5 6 7 8
wangsx@SC-201708020022:~/tmp$ ./test23 members.csv wangsx@SC-201708020022:~/tmp$ cat members.sql INSERT INTO members (lname,fname,address,city,state,zip) VALUES ('Blum', 'Richard', '123 Main St.', 'Chicago', 'IL', '60601'); INSERT INTO members (lname,fname,address,city,state,zip) VALUES ('Blum', 'Barbara', '123 Main St.', 'Chicago', 'IL', '60601'); INSERT INTO members (lname,fname,address,city,state,zip) VALUES ('Bresnahan', 'Timothy', '456', ' Oak Ave.', 'Columbus', 'OH,43201');