UNIX AIXは、配列を使用して名前とパスを保存し、forループを使用してそれぞれを取得します。

UNIX AIXは、配列を使用して名前とパスを保存し、forループを使用してそれぞれを取得します。

一部のファイルのパスと名前を含む配列を生成する必要があるスクリプトがあります。

その後、ユーザーのパラメータが配列の名前の1つと一致することを確認してから、パスと名前を別々に別のスクリプトに渡す必要があります。

array1[0]="/Distenation1/File1.txt"
array1[1]="file1"

#testing another way to set an array
set -A array2 "/Distenation2/File2.txt" file2

配列全体を渡す方法が見つからず、forループでユーザーと一致させる方法が見つからないため、問題が発生します。パラメータ $1そしてファイル1またはファイル2そして、そのパスを別のスクリプトに渡します。

#the following code doesnt work as needed -logically-
for i in ${array1[@]} ${array2[@]}
do
        if [ $1 = ${i[1]} ]
        then
                ./sendfile ${i[0]}
        fi;
done

編集:kshバージョン=バージョンM-11/16/88fのようです。

上記と同じコードを使用しますが、echoを使用して例を示します。

パスワード:

for i in ${array1[@]} ${array2[@]}
do
    echo Name : ${i[1]} '\n'Path : ${i[0]}
done

出力:

Name :
Path : /Distenation1/file1.txt
Name :
Path : file1
Name :
Path : /Distenation2/file2.txt
Name :
Path : file2

必要な結果は次のとおりです。

Name : file1
Path : /Distenation1/file1.txt
Name : file2
Path : /Distenation2/file2.txt

ベストアンサー1

私は理解すると思われる解決策を試しました。

#put all the data inside 1 array
array1[0]="/Distenation1/File1.txt"
array1[1]="file1"
array1[2]="/Distenation2/File2.txt"
array1[3]="file2"

#create a counter    
n=0

#the trick here was that 'i' (loop) stores 1 array data per iteration (at first i thought it stores the whole array data then go through them 1 by one)
for i in ${array1[@]}
do
        if [ $1 = ${i} ]
        then
#here i had to call the original array to be able to read through any data inside the array
                echo Name : $i '\n' Path : ${array1[$n+1]}
        fi;

        let n=n+1
done;

出力:

=>test1 file1
Name : file1
 Path : /Distenation1/File1.txt

これがベストプラクティスであるかどうかはわかりませんが、非常に大きなスクリプトで作業し、毎ミリ秒の最適化が違いを生むので、より良いソリューションや最適化されたコードで開かれています。

おすすめ記事