Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Shell, Execute a command, that needs to go through mutltiple values one by one

I have a script:

if [ $ModEnabled == "1" ];then
    ${HOME}/servers/steamcmd/steamcmd.sh +force_install_dir ${HOME}/servers/dayzserver/ +login "${SteamUser}"  +workshop_download_item 221100 1559212036 validate +quit
    printf "Done"
else
    echo "Variable is not set"
fi

However, what I need to do is to run that command +workshop_download_item 221100 1559212036 validate

Where it re-run/loops the command replacing the specific value "1559212036" with a list of multiple values.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

The vision is: (fake code, obviously lol)

modlist={"
    123123123,
    234456567,
    456456456,
    567567567,"
}

then this runs:

${HOME}/servers/steamcmd/steamcmd.sh +force_install_dir ${HOME}/servers/dayzserver/ +login "${SteamUser}"  +workshop_download_item 221100 $modlist, then run again with the next item in modlist" validate +quit

If it is possible to do this without calling "+quit" everytime, thats a bonus (but I guess this is a steamcmd thing.

>Solution :

The construct you are looking for is a for loop.

if [ $ModEnabled == "1" ];then
    for value in 123123 456456 789789 ; do
        ${HOME}/servers/steamcmd/steamcmd.sh +force_install_dir ${HOME}/servers/dayzserver/ +login "${SteamUser}"  +workshop_download_item 221100 "${value}" validate +quit
    done

    printf "Done"
else
    echo "Variable is not set"
fi

You set the iterator (I’ve used the variable name value, but you can use whatever you like) and then access it using (in this example) "${value}".

You can put all the values together in a variable as well, but it will just be a string:

modlist="123123 456456 789789"

for value in $modlist ; do
    …
done
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading