Medic303
Medic303

Reputation: 3

How can I store a list of arguments in a variable in bash?

Is it possible to make this file cleaner by not having to type out 19 different variables. I tried putting all the paths in the same variable but I get permission errors (edited to show code I was using at the bottom). Making them variables for each path removed that permission issue. Then as you can see in the bottom comment, I was looking at a way to include all variables without having to type out what currently is working. Not sure if that would work.

# Reverts permissions from updating

ocpath='/var/www/html/core'
ocpath1='/var/www/html/lib'
ocpath2='/var/www/html/ocs'
ocpath3='/var/www/html/ocs-provider'
ocpath4='/var/www/html/resources'
ocpath5='/var/www/html/settings'
ocpath6='/var/www/html/AUTHORS'
ocpath7='/var/www/html/CHANGELOG.md'
ocpath8='/var/www/html/console.php'
ocpath9='/var/www/html/COPYING'
ocpath10='/var/www/html/cron.php'
ocpath11='/var/www/html/db_structure.xml'
ocpath12='/var/www/html/index.html'
ocpath13='/var/www/html/index.php'
ocpath14='/var/www/html/occ'
ocpath15='/var/www/html/public.php'
ocpath16='/var/www/html/remote.php'
ocpath17='/var/www/html/robots.txt'
ocpath18='/var/www/html/status.php'
ocpath19='/var/www/html/version.php'

htuser='someuser'
htgroup='someusergroup'

chown -R ${htuser}:${htgroup} $ocpath $ocpath2 $ocpath3 $ocpath4 $ocpath5 $ocpath6 $ocpath7 $ocpath8 $ocpath9 $ocpath10 $ocpath11 $ocpath12 $ocpath13 $ocpath14 $ocpath$15 $ocpath16 $ocpath17 $ocpath18 $ocpath19

# chown -R ${htuser}:${htgroup} $ocpath1:$ocpath19
$ocpath='/var/www/html/filename' '/var/www/html/filename2' '/var/www/html/filename3'

Upvotes: 0

Views: 1450

Answers (2)

William Pursell
William Pursell

Reputation: 212198

You can use those new-fangled arrays, but if you're limited to using a light weight shell without arrays, or need to conform to a standard that doesn't allow such constructions, you can do:

#!/bin/sh

set -- # Clear the positional arguments
for t in core lib ocs ocs-provider resources settings AUTHORS \
                CHANGELOG.md console.php COPYING cron.php \
                db_structure.xml index.html \
                index.php occ public.php remote.php \
                robots.txt status.php \
                version.php; do
        set -- "$@" "/var/www/html/$t"
done
htuser=someuser
htgroup=someusergroup

chown -R ${htuser}:${htgroup} "$@"

Note, as a side effect this clears the positional arguments Alhtough, honestly, you might just want chown "$htuser:$htgroup" -R /var/www/html/*, or (more reasonably) call chown in the loop and avoid all the nastiness of the quoting rules.

Upvotes: 0

chepner
chepner

Reputation: 530920

This is what arrays are for.

ocpaths=(
  '/var/www/html/core'
  '/var/www/html/lib'
  '/var/www/html/ocs'
  '/var/www/html/ocs-provider'
  '/var/www/html/resources'
  # etc
)

htuser='someuser'
htgroup='someusergroup'

chown -R "${htuser}:${htgroup}" "${ocpaths[@]}"

Upvotes: 4

Related Questions