0

In Ubuntu 16.04 with Bash I had a problem when I didn't have a convenient way to upgrade all my WordPress components (core, translations, theme, plugin) and I used the following code to solve it:

cat <<-EOF > /etc/cron.daily/cron_daily
    #!/bin/bash
    drt="/var/www/html"
    for dir in "$drt/*/"; do
        if pushd "$dir"; then
            wp plugin update --all --allow-root
            wp core update --allow-root
            wp language core update --allow-root
            wp theme update --all --allow-root
            rse
        popd
        fi
    done
EOF

What I'd like to ask is actually comprised of these questions:

  1. How does the dir variable resembles the asterisk (*) coming a little bit after it in the same line (how is the Bash interpreter knows that dir represents each directory inside /var/www/html?

  2. How does the pushd-popd sequence(?) works here? I understand it to "if you are inside $dir which resembles each iteration of the for loop, do stuff".

  • Not clear to me what you are asking. drt is set somewhere (apparently to /var/www/html/), and $drt/* simply expands to everything in that directory. With pushd you try to enter the directory, and if that works, you do some stuff, and with popd you return to your previous directory. – pfnuesel Mar 14 '18 at 16:56
3

How does the dir variable resembles the asterisk (*) coming a little bit after it in the same line (how is the Bash interpreter knows that $dir [iteratively] represents each directory inside /var/www/html/?

That's just how Bash shell globs work/behave, but you are mistaken about one thing: dir/* is a glob which includes all files within dir, not just all directories: in a POSIX environment, directories are a file type, but in this case only directories are relevant to the for loop and the subsequent pushd-popd pair.

How does the pushd-popd sequence(?) works here? I understand it to "if you are inside $dir which resembles each iteration of the for loop, do stuff".

pushd and popd are a pair of tools that work with a data structure known as a "stack". Picture a spring-loaded dispenser, like a Pez dispenser.

If you push an item onto a stack, you are storing it for later use, like pushing one 'pill' into the top of the Pez dispenser.

If you pop an item from a stack, you are taking it out for use or reference. This removes it from the stack.

Take a look at this behavior for a simple example of how pushd and popd work:

$ pwd
/home/myuser
$ pushd /etc
/etc ~
$ pwd
/etc
$ pushd /usr/local
/usr/local /etc ~
$ pwd
/usr/local
$ popd
/etc ~
$ pwd
/etc
$ popd
~
$ pwd
/home/myuser

Your for loop basically works by saying if I can push this directory onto the directory stack, then that means firstly that the directory now exists, and secondly that that directory is now the current working directory. It will proceed to do the work, and then popd to go back to wherever it had been before, and then run the next iteration of the loop.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.