ifs

if-for-while

筅森魡賤 提交于 2019-12-27 14:02:41
if help if可以看看if的用法 if ls -l / ;then echo "ok";else echo "no" ;fi for for ((i=0;i<10;i++));do echo $i; done help for里面的words:asd asd asd,word:asd for i in aaa fff ssss aaaa;do echo $i;done 命令seq,比如seq 33:从1输出到33 for i in `seq 33`;do echo $i;done while help while 练习 先统计一下,并用数值法并倒过来排个序:du -a | sort -nr 思想:把统计结果逆序排列,然后开始循环,把第一个是文件的输出即可。 编程: oldIFS=¥IFS:bash是通过IFS里面的值来进行切割的,之前存的是空格,换行,制表符,这次要换成换行符,不过先保存现场。 IFS=$'\n':加单引号是为了使得\和n合为一体,$表示取它的值 接着``里面是按照逆序r排列的,对用户输入的路径的所有文件和目录的大小信息的数据集,增强for循环遍历里面的每一个元素。 for i in `du -a $1 | sort -nr`; do   echo $1   filename=`echo i | awk '{print $2}'` :

Creating multiple loops with string read from ascii files in bash

狂风中的少年 提交于 2019-12-24 22:42:41
问题 I want to create an internal loop inside a loop with strings which are read from ascii files in a bash script. I have two input ascii files which contain a list of strings: files.txt: filename1 filename2 polls.txt: CO NOx SOx I used IFS and read as following : while IFS= read -r file; do printf '%s\n' "$file" while IFS= read -r poll; do printf '%s\n' "$poll" ogrinfo $Database_maindir/$file".shp" -sql "ALTER TABLE $file ADD COLUMN $poll float" done < "$Database_maindir/polls.txt" done < "

CSV parse with IFS bash : choose “;” as the separator

限于喜欢 提交于 2019-12-24 01:58:37
问题 I have a CSV with 130 cols and i need to do 3 csv with that. I'm looping with a while and IFS because i need to do someting with the vars on each row. Here what i did : while IFS=";" read [my 130 vars] [what i do with the vars] done < file.csv But i have a problem on some rows because the original csv i receive is like : "Hi";"i";"got;a problem" As you can see i have a problem with a ; in a value. And the IFS read it as the separation of two values. So here is my question : is there a way to

Setting IFS for a single statement

点点圈 提交于 2019-12-21 14:12:11
问题 On my GNU bash, version 4.3.42(1)-release I am doing some tests to answer a question. The idea is to split a : -separated string and and each of its elements into an array. For this, I try to set the IFS to : in the scope of the command, so that the split is automatic and IFS remains untouched: $ myvar="a:b:c" $ IFS=: d=($myvar) $ printf "%s\n" ${d[@]} a b c And apparently IFS remains the same: $ echo $IFS # empty The BASH reference says that: If IFS is unset, the parameters are separated by

shell - temp IFS as newline only. Why doesn't this work: IFS=$(echo -e '\n')

安稳与你 提交于 2019-12-21 13:03:00
问题 I'm trying to use for in the shell to iterate over filenames with spaces. I read in a stackoverflow question that IFS=$'\n' could be used and that seems to work fine. I then read in a comment to one of the answers that to make it posix compatible for shells other than bash one could use IFS=$(echo -e '\n') . The former works but the latter doesn't. The comment is upvoted several times. I checked the output and the variable doesn't seem to contain the newline. #!/bin/sh IFS=$(echo -e '\n')

IFS separate a string like “Hello”,“World”,“this”,“is, a boring”, “line”

跟風遠走 提交于 2019-12-20 07:41:13
问题 I'm trying to parse a .csv file and I have some problems with IFS. The file contains lines like this: "Hello","World","this","is, a boring","line" The columns are separated with a comma, so I tried to explode the line with this code: IFS=, read -r -a tempArr <<< "$line" But I get this output: "Hello" "World" "this" "is a boring" "line" I understand why, so I tried some other commands but I don't get my expected output. IFS=\",\" IFS=\", IFS=',\"' IFS=,\" Every time the third element is

shell脚本编程-循环(for、while、until)

試著忘記壹切 提交于 2019-12-17 19:59:59
for命令格式: – list参数:迭代中要用的一系列值 – 每个迭代中,变量var会包含列表中的当前值 – do和done语句之间输入的命令可以是一条或多条标准的bash shell命令 1 2 3 4 for var in list do commands done 读取列表中的值 for命令最基本的用法就是遍历for命令自身中定义的一系列值: 在最后一次迭代后,$test变量的值会在shell脚本的剩余部分一直保持有效,除非修改它 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 $cat test #!/bin/bash for test in Alabama Alaska Arizona Arkansas California Colorado do echo The next state is $test done echo "The last state we visited was $test" test = Connecticut echo "Wait , now we're visiting was $test" $ . / test The next state is Alabama The next state is Alaska . . . The last state we visited was Colorado wait ,

Shell编程—结构化命令(2)

北慕城南 提交于 2019-12-15 20:28:02
1 for 命令 for命令的基本格式: for var in list do commands done 在list参数中,你需要提供迭代中要用到的一系列值。 1.1 读取列表中的值 例子: $ vim test1 #!/bin/bash # testing the for variable after the looping for test in Alabama Alaska Arizona Arkansas California Colorado do echo "The next state is $test" done echo "The last state we visited was $test" test=Connecticut echo "Wait, now we're visiting $test" 执行结果: $ ./test1 The next state is Alabama The next state is Alaska The next state is Arizona The next state is Arkansas The next state is California The next state is Colorado The last state we visited was Colorado Wait, now we're

c++ 文件操作

假装没事ソ 提交于 2019-12-14 04:52:26
#include <iostream> #include <fstream> //写文本文件 void test01() { //创建流对象 std::ofstream ofs; //指定打开方式 ofs.open("test.txt",std::ios::out); //写内容 ofs<<"xiaoming"<<std::endl; ofs<<"man"<<std::endl; ofs<<"18"<<std::endl; //关闭文件 ofs.close(); } //读文本文件 void test02() { //创建流对象 std::ifstream ifs; //打开文件 并且判断是否打开成功 ifs.open("test.txt",std::ios::in); if(!ifs.is_open()) { std::cout<<"文件打开失败!"<<std::endl; return; } //读数据 // //第一种 // char buf1[1024] = {0}; // while (ifs>>buf1) // { // std::cout<<buf1<<std::endl; // } // //第二种 // char buf2[1024] = {0}; // while(ifs.getline(buf2, sizeof(buf2))) // { // std:

How to escape a variable in Bash when passing to a command-line argument

纵然是瞬间 提交于 2019-12-14 02:53:30
问题 I've got a Bash script (Cygwin) that uses some Windows paths with spaces in them. Consequently, I have escaped the space with a \ in my variable definition. Everything within the script works fine. However, I need to pass this variable as an argument to a command-line executable. When I do that, my escaping gets messed up. Sample non-functional script: #!/bin/sh # File paths destinationPath="/cygdrive/c/Documents and Settings/Eric/My Documents/" attachments="\n2013-12-12.pdf" body="Test Body"