How does pushd work?
Clash Royale CLAN TAG#URR8PPP
up vote
0
down vote
favorite
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:
How does the
dir
variable resembles the asterisk (*
) coming a little bit after it in the same line (how is the Bash interpreter knows thatdir
represents each directory inside/var/www/html
?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".
bash upgrade for syntax pushd
add a comment |Â
up vote
0
down vote
favorite
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:
How does the
dir
variable resembles the asterisk (*
) coming a little bit after it in the same line (how is the Bash interpreter knows thatdir
represents each directory inside/var/www/html
?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".
bash upgrade for syntax pushd
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. Withpushd
you try to enter the directory, and if that works, you do some stuff, and withpopd
you return to your previous directory.
â pfnuesel
Mar 14 at 16:56
add a comment |Â
up vote
0
down vote
favorite
up vote
0
down vote
favorite
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:
How does the
dir
variable resembles the asterisk (*
) coming a little bit after it in the same line (how is the Bash interpreter knows thatdir
represents each directory inside/var/www/html
?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".
bash upgrade for syntax pushd
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:
How does the
dir
variable resembles the asterisk (*
) coming a little bit after it in the same line (how is the Bash interpreter knows thatdir
represents each directory inside/var/www/html
?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".
bash upgrade for syntax pushd
edited Mar 14 at 17:17
asked Mar 14 at 16:32
user9303970
123224
123224
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. Withpushd
you try to enter the directory, and if that works, you do some stuff, and withpopd
you return to your previous directory.
â pfnuesel
Mar 14 at 16:56
add a comment |Â
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. Withpushd
you try to enter the directory, and if that works, you do some stuff, and withpopd
you return to your previous directory.
â pfnuesel
Mar 14 at 16:56
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 at 16:56
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 at 16:56
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
3
down vote
accepted
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.
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
accepted
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.
add a comment |Â
up vote
3
down vote
accepted
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.
add a comment |Â
up vote
3
down vote
accepted
up vote
3
down vote
accepted
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.
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.
edited Mar 14 at 19:46
answered Mar 14 at 17:03
DopeGhoti
40.2k54779
40.2k54779
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f430220%2fhow-does-pushd-work%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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. Withpushd
you try to enter the directory, and if that works, you do some stuff, and withpopd
you return to your previous directory.â pfnuesel
Mar 14 at 16:56