シェル配列(添え字でアクセス)

(24)


cat <<-'EOF' > a.conf

HOST=(
"test@192.168.137.149"
"test@192.168.137.181"
)

COM=(
"uname -n"
"pwd"
)

EOF

cat <<-'EOF' > a.sh

#!/bin/bash


SCRIPT_DIR=$(cd $(dirname $0); pwd)
echo ${SCRIPT_DIR}
source ${SCRIPT_DIR}/a.conf


echo ${#HOST[@]}

echo ${HOST[0]}
echo ${HOST[1]}

echo ${COM[0]}
echo ${COM[1]}


for i in "${!HOST[@]}" ; do
    echo "HOST $i => ${HOST[$i]}"
    echo "COM $i => ${COM[$i]}"
    
    ssh "${HOST[$i]}" "${COM[$i]}"
    echo ""
done

EOF

chmod +x a.sh


cat a.conf
cat a.sh

./a.sh

 

 

(12)

 


cat <<-'EOF' > a.conf

HOST=(
"root@192.168.137.187"
"root@192.168.137.189"
)

COM=(
"uname -n"
"pwd"
)

EOF

cat <<-'EOF' > a.sh

#!/bin/bash


SCRIPT_DIR=$(cd $(dirname $0); pwd)
echo ${SCRIPT_DIR}
source ${SCRIPT_DIR}/a.conf


echo ${#HOST[@]}

echo ${HOST[0]}
echo ${HOST[1]}

echo ${COM[0]}
echo ${COM[1]}


for i in "${!HOST[@]}" ; do
    echo "HOST $i => ${HOST[$i]}"
    echo "COM $i => ${COM[$i]}"
    
    ssh "${HOST[$i]}" "${COM[$i]}"
    echo ""
done

EOF

chmod +x a.sh


cat a.conf
cat a.sh

./a.sh

 

 

https://anotools.com/shell/802/
https://amano41.hatenablog.jp/entry/bash-for-loop-over-array
https://qiita.com/koara-local/items/2d67c0964188bba39e29


cat <<-'EOF' > a.conf

HOST=(
"root@192.168.137.190"
"root@192.168.137.191"
)

COM=(
"uname -n"
"pwd"
)

EOF

cat <<-'EOF' > a.sh

#!/bin/bash


SCRIPT_DIR=$(cd $(dirname $0); pwd)
echo ${SCRIPT_DIR}
source ${SCRIPT_DIR}/a.conf


echo ${#HOST[@]}

echo ${HOST[0]}
echo ${HOST[1]}

echo ${COM[0]}
echo ${COM[1]}


for i in "${!HOST[@]}" ; do
    echo "HOST $i => ${HOST[$i]}"
    echo "COM $i => ${COM[$i]}"
    
    ssh "${HOST[$i]}" "${COM[$i]}"
    echo ""
done

EOF

chmod +x a.sh


cat a.conf
cat a.sh

./a.sh

 

 

(2022)

https://tex2e.github.io/blog/powershell/array
https://qiita.com/nokachiru/items/b47cc541f82b5cac7e37
https://superuser.com/questions/1516905/what-is-the-eof-equivalent-in-powershell

 


@'

$HOSTMEI=@(
"root@192.168.137.190",
"root@192.168.137.191"
)

$COM=@(
"uname -n",
"pwd"
)

'@ | Tee-Object -FilePath a.ps1 


@'

$INI_PATH = @(Split-Path $script:myInvocation.MyCommand.Path -Parent).Trim()
$INI_FILE = "a.ps1"
$INI_FULLNAME = $INI_PATH + "\" + $INI_FILE

Write-Host $INI_FULLNAME

. $INI_FULLNAME


Write-Host $HOSTMEI.count

Write-Host $HOSTMEI[0]
Write-Host $HOSTMEI[1]

Write-Host $COM[0]
Write-Host $COM[1]

 

for ( $i = 0; $i -lt $HOSTMEI.count; $i++){
    Write-Host "HOST $i =>"  $HOSTMEI[$i]
    Write-Host "COM $i => "  $COM[$i]
    ssh $HOSTMEI[$i] $COM[$i]
    Write-Host ""
}

'@ | Tee-Object -FilePath b.ps1 

cat a.ps1
cat b.ps1

.\b.ps1