shell获取字符串

时间:2013-02-05 02:01:10

标签: bash shell

我有一些行具有相同的结构,如

1000    AS34_59329    RICwdsRSYHSD11-2-IPAAPEK-93     /ifshk5/BC_IP/PROJECT/T1
1073/T11073_RICekkR/Fq/AS34_59329/111220_I631_FCC0E5EACXX_L4_RICwdsRSYHSD11-2-IP
AAPEK-93_1.fq.gz        /ifshk5/BC_IP/PROJECT/T11073/T11073_RICekkR/Fq/AS34_5932
9/111220_I631_FCC0E5EACXX_L4_RICwdsRSYHSD11-2-IPAAPEK-93_2.fq.gz        /ifshk5/
BC_IP/PROJECT/T11073/T11073_RICekkR/Fq/AS34_59329/clean_111220_I631_FCC0E5EACXX_
L4_RICwdsRSYHSD11-2-IPAAPEK-93_1.fq.gz.total.info       11.824  0.981393
43.8283 95.7401 OK

我希望得到Bold部分来检查/home/jesse/中是否有此文件夹,如果没有创建mkdir /home/jesse/AS34_59329

我使用此代码

  ! /bin/bash
  myPath="/home/jesse/"
  while read myline
  do
     dirname= echo "$myline" | awk -F ' ' '{print $2}'
     echo $dirname
     myPath= $myPath$dirname
     echo $myPath
     mkdir -p "$myPath"

  done < T11073_all_3254.fq.list

但它不能mkdir并显示路径名称,它显示

 -bash: /home/jesse/: is a directory
 /home/jesse/
 AS39_59324

2 个答案:

答案 0 :(得分:1)

read可以将每个字段读入单独的变量,mkdir -p只有在不存在的情况下才会创建目录:

path="/home/jesse"
while read _ dir _ 
do 
    mkdir -p "$path/$dir"
done < T11073_all_3254.fq.list

答案 1 :(得分:0)

for将迭代每个空格分隔的标记。试试这个。

#!/usr/bin/env bash
# Invoke with first arg as file containing the lines
#     foo.sh <input_filename>

for i in `cat $1 | cut -d " " -f2`
do 
    if [ -d /home/jesse/$i ]
    then
        echo "Directory /home/jesse/$i exists"
    else
        mkdir /home/jesse/$i;
        echo "Directory /home/jesse/$i created"
    fi
done