2

コード

s='id;some text here with possible ; inside'
IFS=';' read -r id string <<< "$s"
echo "$id"

エラー

restore.sh: 2: restore.sh: Syntax error: redirection unexpected

バッシュのバージョンGNU bash, version 4.2.37(1)-release (x86_64-pc-linux-gnu)

4

5 に答える 5

11

ヒア文字列は、小さなヒア ドキュメントの単なるショートカットです。これは、どの POSIX シェルでも機能するはずです。

s='id;some text here with possible ; inside'
IFS=';' read -r id string <<EOF
$s
EOF
echo "$id"
于 2013-11-12T14:38:23.757 に答える
1

ファイル分割で保存

#!/bin/sh

string=$1
c=0

while [ $c -le $3 ] 
do

val1=`echo "$string" |sed 's/\(^[^'$2']*\)\('$2'\+\)\(.*$\)/\1/'`
string=`echo "$string" |sed 's/\(^[^'$2']*\)\('$2'\+\)\(.*$\)/\3/'`

if [ "$val1" == "$string" ]
then
string=''
fi

if [ "$val1" == "" ]
then
let c=$3
fi

let c=c+1

done

echo $val1 

そしてそれは動作します:

root@IPHONE:~# ./split 'a;b;c' ';' 0
a
root@IPHONE:~# ./split 'a/b/c' '\/' 0
a
root@IPHONE:~# ./split 'a/b/c' '\/' 2
c
root@IPHONE:~# ./split 'a/b/c' '\/' 1
b
于 2018-08-18T14:19:10.277 に答える
-1

私はpython2.7以下を使用しました:

#!/bin/sh

s='id;some text here with possible ; inside'
python -c "ifc, s =';', '$s';print s.split(';')[0] if ifc in s else ''"

結果:

 $ ./test.sh
id
于 2013-11-12T14:34:41.230 に答える