-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3-directories.sh
More file actions
1706 lines (1328 loc) · 38.1 KB
/
3-directories.sh
File metadata and controls
1706 lines (1328 loc) · 38.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/sh
. .lib/shell-compat-test.sh
_DURATION=15
_LSN_VERSION=1.0.0
# Put tutorial library files into $PATH if they are not already added
if [[ -d "$PWD/.lib" && ":$PATH:" != *":$PWD/.lib:"* ]]; then
PATH=$PWD/.lib:$PATH
fi
source ansi-terminal-ctl.sh
source progress.sh
if [[ -n $_TUTR ]]; then
source generic-error.sh
source noop.sh
source open.sh
source platform.sh
_err() { (( $# == 0 )) && echo $(red _err) || echo $(red "$*"); }
fi
ask_to_open_explorer_msg() {
cat <<-:
In this lesson you will create, delete, and navigate through folders in
the shell. You can follow along in a graphical file explorer window.
:
}
ask_to_open_explorer() {
# Only ask to open a file explorer the very 1st time this step is encountered
if [[ ${_EXPLORER-unset} = unset ]]; then
_tutr_info ask_to_open_explorer_msg
if _tutr_yesno "Would you like to open a file explorer?"; then
_tutr_open "$_BASE"
_EXPLORER=yes
else
_tutr_open_init # set value $_OPEN for a later message
_EXPLORER=
fi
fi
}
_make_files() {
mkdir -p "$_BASE"/music/genre{0,1,2}/artist{0,1,2}/album{0,1,2}
touch "$_BASE"/music/genre{0,1,2}/artist{0,1,2}/album{0,1,2}/track_0{1,2,3,4,5,6,7,8,9}.mp3
touch "$_BASE/a_file"
}
setup() {
source screen-size.sh 80 30
export _BASE="$PWD/lesson3"
[[ -d "$_BASE" ]] && rm -rf "$_BASE"
mkdir -p "$_BASE"
_make_files
}
prologue() {
[[ -z $DEBUG ]] && clear
echo
cat <<-PROLOGUE
$(_tutr_progress)
Shell Lesson #3: Directories
In this lesson you will learn how to
* Navigate directories
* Create new directories
* Remove empty directories
* Forcibly remove directories without regard for their contents
This lesson takes around $_DURATION minutes.
PROLOGUE
_tutr_pressenter
}
cd_music_rw() {
cd "$_BASE"
}
cd_music_ff() {
cd "$_BASE/music"
}
cd_music_pre() {
# Determine whether their prompt actually displays the CWD
# * Can't run this in `setup()` because that runs in the context
# of the shell in the shebang line, /bin/sh, and is non-interactive
# (i.e. no PS1)
# * Can't run this in `prologue()` because that runs in a subshell & will
# discard _PS1_CWD before I can see it
# * Hence, we run it here
# Zshmisc: CWD prompt escape sequences: %d, %/, %~
# Bash: CWD prompt escape sequences: \w, \W
if [[ -n $ZSH_NAME ]]; then
if [[ $PS1 =~ %[0-9]*[~c.] ]]; then _PS1_CWD=tilde
elif [[ $PS1 =~ %[0-9]*[d/C] ]]; then _PS1_CWD=full
fi
else
[[ $PS1 =~ \\[wW] ]] && _PS1_CWD=tilde
fi
ask_to_open_explorer
}
cd_music_prologue() {
if [[ -n $ZSH_NAME && $_EXPLORER = yes ]]; then
cat <<-:
You will see some lines of output that look like this:
$(bld "[12] $$")
$(bld "[12] + $$ done $_OPEN $_BASE")
Just ignore this.
:
_tutr_pressenter
echo
fi
if [[ $_EXPLORER = yes ]]; then
cat <<-:
Resize the file explorer window so you can comfortably watch it and
this terminal at the same time.
As you move through the folders in the shell, follow along in the file
explorer by clicking the corresponding folders. Use the $(kbd Back) or $(kbd Up)
buttons to go out of a folder.
If you accidentally close the file explorer window and want
it back, just run $(cmd $_OPEN .) ($(bld "note the dot at the end!")).
Of course, you can also see folders in the shell with $(cmd ls).
:
else
cat <<-:
As usual, you will be able to see the folders with $(cmd ls).
If you change your mind later, this command brings up the file explorer:
$(cmd $_OPEN .)
($(bld "note the dot at the end!"))
:
fi
_tutr_pressenter
cat <<-:
A directory is a collection of files and other directories. You may be
familiar with the concept of a $(bld folder) in a graphical file explorer;
$(cyn folders are exactly the same things as directories).
Directories are locations that your programs can be $(bld in.) When a program
is $(bld in) a directory, it can access the files and directories there.
The shell you are running right now is in a directory. This directory
holds another directory named $(path music) and a file named $(path a_file). Use $(cmd ls) to
see for yourself.
:
_tutr_pressenter
cat <<-:
You can change into a different directory with the $(cmd cd) command.
Its syntax is
$(cmd 'cd [DIRECTORY|-]')
The '$(bld '|')' means $(bld "or"). The syntax above means
"$(cmd cd) $(cyn may be) $(cyn run with no arguments,)
$(bld OR) $(cyn it can be given a DIRECTORY)
$(bld OR) $(cyn it can take a dash) $(bld -) $(cyn as its argument)"
Use $(cmd cd) to enter the $(path music) directory.
:
}
cd_music_test() {
if [[ "$PWD" = "$_BASE/music" ]]; then return 0
elif _tutr_noop; then return $NOOP
else _tutr_generic_test -c cd -a music -d "$_BASE"
fi
}
cd_music_hint() {
_tutr_generic_hint $1 cd "$_BASE/music"
[[ $1 = $WRONG_PWD ]] && return
cat <<-:
Use the $(cmd cd) command to enter the $(path music) directory.
:
}
cd_music_post() {
_PREV=${_CMD[0]}
}
# cd into genre1
cd_genre1_rw() {
cd "$_BASE/music"
}
cd_genre1_ff() {
cd "$_BASE/music/genre1"
}
cd_genre1_prologue() {
if [[ -n ${_PS1_CWD:-} ]]; then
cat <<-:
You are now in the directory $(path music). Notice that your shell's prompt
looks different now than before you ran $(cmd $_PREV).
:
else
cat <<-:
You are now in the directory $(path music).
:
fi
if [[ $_EXPLORER = yes ]]; then
cat <<-:
Double-click the $(path music) folder icon in the file explorer to follow along.
:
fi
cat <<-:
This directory happens to contain my illicit MP3 collection. Don't
worry about getting in trouble - I've cleverly disguised the files so
the copyright holders cannot identify their intellectual property.
A directory contained inside another directory is called a
$(bld subdirectory). There is only one directory in a Unix system that is
technically not a $(bld subdirectory) (more on that in a moment), so most
of the time these two terms are used interchangeably.
If you run $(cmd ls) again you will see more subdirectories.
Use the $(cmd cd) command to enter the $(path genre1) subdirectory.
By the way, you can use $(kbd '<TAB>') autocompletion on subdirectories.
:
}
cd_genre1_test() {
if [[ "$PWD" = "$_BASE/music/genre1" ]]; then return 0
elif _tutr_noop; then return $NOOP
else _tutr_generic_test -c cd -a genre1 -d "$_BASE/music"
fi
}
cd_genre1_hint() {
_tutr_generic_hint $1 cd "$_BASE/music/genre1"
[[ $1 = $WRONG_PWD ]] && return
cat <<-:
Use the $(cmd cd) command to enter the $(path genre1) subdirectory.
:
}
# cd into artist0/album2
cd_artist0_album2_rw() {
cd "$_BASE/music/genre1"
}
cd_artist0_album2_ff() {
cd "$_BASE/music/genre1/artist0/album2"
}
cd_artist0_album2_prologue() {
if [[ $_EXPLORER = yes ]]; then
cat <<-:
Now open the $(path genre1) folder icon in the file explorer.
:
fi
cat <<-:
You were able to enter this directory because the shell was $(bld in) the
directory named $(path music) and $(path genre1) was one of its subdirectories.
$(path genre1) itself contains some subdirectories, each of which contain
more subdirectories, and so on.
Considering subdirectories to be $(bld children) makes the directory that
contains them the $(bld parent). The directory $(path music) is the $(bld parent)
directory of $(path genre1).
You can navigate the directory structure by traversing one directory at
a time from parent to child.
:
_tutr_pressenter
cat <<-:
If you already know how deep you want to go, you can give $(cmd cd) multiple
subdirectories at once by giving the names of related directories
separated by $(bld front slashes) $(path /). $(bld Front slash) is the symbol that shares a
key with question mark $(kbd '?') (the other slash is called $(bld back slash)).
$(bld Example:) $(path artist0/album2) is a $(bld grandchild) directory of $(path genre1).
:
if [[ $_EXPLORER = yes ]]; then
cat <<-:
In a graphical file explorer, this same operation is accomplished
with $(bld extra clicking).
:
fi
cat <<-:
Use a single $(cmd cd) command to directly enter the $(path artist0/album2)
subdirectory. Let $(kbd '<TAB>') completion save you from tedious typing.
:
}
cd_artist0_album2_test() {
if [[ "$PWD" = "$_BASE/music/genre1/artist0/album2" ]]; then return 0
elif [[ "$PWD" = "$_BASE/music/genre1/artist0" ]]; then return 99
elif _tutr_noop; then return $NOOP
else _tutr_generic_test -c cd -a artist0/album2 -d "$_BASE/music/genre1"
fi
}
cd_artist0_album2_hint() {
case $1 in
99)
cat <<-:
You're halfway there.
Now go into the directory $(path album2).
:
;;
*)
_tutr_generic_hint $1 cd "$_BASE/music/genre1/artist0/album2"
[[ $1 = $WRONG_PWD ]] && return
cat <<-:
Use the $(cmd cd) command to enter the $(path artist0/album2) subdirectory.
$(cmd cd artist0/album2)
Let $(kbd '<TAB>') completion save you from tedious typing.
:
;;
esac
}
cd_artist0_album2_epilogue() {
if [[ ${_PS1_CWD:-} ]]; then
cat <<-:
You will have noticed that your shell's prompt has been changing as you
move between directories. This is to remind you of the shell's current
location.
:
fi
cat <<-:
The location that a program runs in is called its "$(bld Current Working)
$(bld Directory)" ($(bld CWD) for short). $(bld CWD) is not just for shells; every program
running on your computer has its own $(bld CWD).
:
_tutr_pressenter
}
cd_dot_dot0_rw() {
cd "$_BASE/music/genre1/artist0/album2"
}
cd_dot_dot0_ff() {
cd "$_BASE/music/genre1/artist0"
}
cd_dot_dot0_prologue() {
if [[ $_EXPLORER = yes ]]; then
cat <<-:
In the graphical file explorer, double-click through $(path artist0) then $(path album2)
to arrive here.
:
fi
cat <<-:
You've reached the end of the line; there are no more subdirectories to
follow. Now that you are here, you can use $(cmd ls) to see the $(cyn MP3s).
When you've gone into a subdirectory, how do you go back out of it?
No matter what your $(bld CWD) is, its parent directory is always called '$(path ..)'.
In other words, you can $(bld always) return to the parent directory with
$(cmd cd ..)
Run that to return to the parent directory.
:
}
cd_dot_dot0_test() {
if _tutr_noop; then return $NOOP
elif [[ "$PWD" = "$_BASE/music/genre1/artist0" ]]; then return 0
else _tutr_generic_test -c cd -a .. -d "$_BASE/music/genre1/artist0/album2"
fi
}
cd_dot_dot0_hint() {
_tutr_generic_hint $1 cd "$_BASE/music/genre1/artist0"
[[ $1 = $WRONG_PWD ]] && return
cat <<-:
Run $(path cd ..) to return to the parent directory.
You want to end up in the directory named $(path artist0)
:
}
cd_dot_dot0_post() {
_PREV_CMD="${_CMD[@]}"
}
# cd .. to go up to genre1
cd_dot_dot1_rw() {
cd "$_BASE/music/genre1/artist0"
}
cd_dot_dot1_ff() {
cd "$_BASE/music/genre1"
}
cd_dot_dot1_prologue() {
if [[ $_EXPLORER = yes ]]; then
case $_OS in
MacOSX)
cat <<-:
Press $(kbd Command-Up Arrow) in the graphical file explorer
to get out of a subdirectory.
:
;;
*)
cat <<-:
Look for an $(kbd Up) button in the graphical file explorer to get out of here.
You may also click the name of the parent directory in the address bar.
:
;;
esac
fi
if [[ "$_PREV_CMD" == "cd .." ]]; then
cat <<-:
Run $(cmd cd ..) again to return to $(path genre1).
:
else
cat <<-:
In the shell, go up by one more level to land in $(path genre1).
:
fi
}
cd_dot_dot1_test() {
if [[ "$PWD" = "$_BASE/music/genre1" ]]; then return 0
elif _tutr_noop; then return $NOOP
else _tutr_generic_test -c cd -a .. -d "$_BASE/music/genre1/artist0"
fi
}
cd_dot_dot1_hint() {
_tutr_generic_hint $1 cd "$_BASE/music/genre1"
[[ $1 = $WRONG_PWD ]] && return
cat <<-:
Use $(cmd cd ..) to return to this directory's parent named $(path genre1).
:
}
# cd ../.. to go up two, back to $_BASE
cd_dot_dot2_rw() {
cd "$_BASE/music/genre1"
}
cd_dot_dot2_ff() {
cd "$_BASE"
}
cd_dot_dot2_prologue() {
if [[ $_EXPLORER = yes ]]; then
case $_OS in
MacOSX)
cat <<-:
Press $(kbd Command-Up Arrow) in the graphical file explorer
to get out of a subdirectory.
:
;;
*)
cat <<-:
Look for an $(kbd Up) button in the graphical file explorer to get out of here.
You may also click the name of the parent directory in the address bar.
:
;;
esac
fi
cat <<-:
Moving one directory at a time is tedious.
As before, you can give $(cmd cd) multiple directories in one command by
separating them with $(path /); the directory name $(path ..) is no exception.
Leave $(path music) entirely and return to the $(path lesson3) directory. You can
do this in a single $(path cd) command by joining two $(path ..) with $(path /):
$(cmd cd ../..)
:
}
cd_dot_dot2_test() {
_IN_MUSIC=99
if [[ "$PWD" = "$_BASE" ]]; then return 0
elif [[ "$PWD" = "$_BASE/music" ]]; then return $_IN_MUSIC
elif _tutr_noop; then return $NOOP
else _tutr_generic_test -c cd -a ../.. -d "$_BASE/music/genre1"
fi
}
cd_dot_dot2_hint() {
case $1 in
$_IN_MUSIC)
cat <<-:
Just about there.
Go up by one more parent directory:
$(cmd cd ..)
:
;;
*)
_tutr_generic_hint $1 cd "$_BASE"
[[ $1 = $WRONG_PWD ]] && return
cat <<-:
Use a single $(cmd cd) command to leave the $(path music) directory and go back to
$(path lesson3). Join two $(path ..) together with a front slash $(path /), like this:
$(cmd cd ../..)
:
;;
esac
}
# cd to go home
cd_home_rw() {
cd "$_BASE"
}
cd_home_ff() {
cd
}
cd_home_prologue() {
if [[ $_EXPLORER = yes ]]; then
case $_OS in
MacOSX)
cat <<-:
Press $(kbd Command-Up Arrow) twice to return to the $(path lesson3) directory in the
graphical file explorer.
:
;;
*)
cat <<-:
Return to the $(path lesson3) directory in the graphical file explorer.
:
;;
esac
fi
cat <<-:
There are two special directories that you should be acquainted with.
The first is your $(bld HOME) directory. Every user on a Unix system has their
own $(bld HOME) directory. Your $(bld HOME) directory is typically named for your
username, and is a subdirectory of $(path /home). When you open a new shell
it usually starts in your $(bld HOME) directory.
When run with no arguments, $(cmd cd) instantly takes you $(bld HOME).
Take this shortcut and go to your $(bld HOME) directory.
:
}
cd_home_test() {
if [[ "$PWD" = "$HOME" ]]; then return 0
elif _tutr_noop; then return $NOOP
else _tutr_generic_test -c 'cd|pushd' -d "$_BASE"
fi
}
cd_home_hint() {
_tutr_generic_hint $1 cd "$HOME"
[[ $1 = $WRONG_PWD ]] && return
cat <<-:
Run $(cmd cd) with no arguments to directly go $(bld HOME).
:
}
cd_home_epilogue() {
cat <<-:
${_M} __,---.__ ${_z}
${_M} ,-' \`-.__ ${_z} This little piggy went
${_M} &/ \`._\ _\ ${_W}
${_M} / ''._ ${_z} Wee, wee, wee,${_z}
${_M} | , (") ${_z}
${_K} jrei${_M} |__,'\`-..--|__|---'' ${_z} all the way $(bld HOME)!
:
_tutr_pressenter
if [[ ${_PS1_CWD:-} ]]; then
cat <<-:
${_B} ~~~~~~~~~ ~~~~~~${_z} In a moment when you return to the shell's
${_B} ~~:::::::::~ ~:::::~${_z} prompt, see how the $(bld CWD) is abbreviated as $(cmd '~').
${_B} ~:::::~~:::::~~:::::~ ${_z} This symbol is called $(bld tilde) (TIL-duh), and is
${_B}~:::::~ ~::::::::::~ ${_z} used as shorthand for your $(bld HOME) directory's
${_B}~~~~~~ ~~~~~~~~~~ ${_z} path both in the shell and in other programs.
:
else
cat <<-:
${_B} ~~~~~~~~~ ~~~~~~${_z} I want to take this opportunity to introduce
${_B} ~~:::::::::~ ~:::::~${_z} you to a bit of Unix culture. The symbol $(cmd '~')
${_B} ~:::::~~:::::~~:::::~ ${_z} is called $(bld tilde) (TIL-duh), and is used as
${_B}~:::::~ ~::::::::::~ ${_z} shorthand for your $(bld HOME) directory's path
${_B}~~~~~~ ~~~~~~~~~~ ${_z} both in the shell and in other programs.
:
fi
if [[ -d ~/Desktop ]]; then
cat <<-:
For example, your $(path HOME) contains a subdirectory named $(path Desktop).
You can go directly there by running:
:
else
cat <<-:
For example, suppose your $(path HOME) contained a subdirectory named $(path Desktop).
You could go directly there by running:
:
fi
cat <<-:
$(cmd "cd ~/Desktop")
instead of typing out all of this:
$(cmd cd $HOME/Desktop)
:
_tutr_pressenter
cat <<-:
While you don't need to use this fact right now, hang on to it because
it will come up again in a later lesson.
:
_tutr_pressenter
}
# cd - to go back to previous dir
cd_minus0_rw() {
cd
}
cd_minus0_ff() {
cd "$_BASE"
}
cd_minus0_prologue() {
local pattern='^cd$|^pushd$'
if [[ "${_CMD[@]}" =~ $pattern ]]; then
cat <<-:
Less typing is fun, right?
:
fi
if [[ ${_PS1_CWD:-} ]]; then
cat <<-:
What if you want to return to where you came from? You $(bld could) type
out the entire set of directory names separated by slashes. They're
right there in your old prompt. It is easy enough to copy & paste
them into a new command.
:
else
cat <<-:
Now, what if you want to go back where you came from? You $(bld could) type
out the entire set of directory names separated by slashes. But that
takes a lot of typing, not to mention a pretty sharp memory.
:
fi
cat <<-:
Easier still is to run $(cmd cd) with the $(cmd -) (minus) argument. $(cmd cd -) takes
you $(bld BACK) to your previous directory, no matter how long its name.
It's like the "$(bld '<- Back')" button in your web browser.
Try it now. Use $(cmd cd -) to return to this lesson's base directory.
:
}
cd_minus0_test() {
if [[ "$PWD" = "$_BASE" ]]; then return 0
elif _tutr_noop; then return $NOOP
else _tutr_generic_test -c cd -a - -d "$_BASE"
fi
}
cd_minus0_hint() {
_tutr_generic_hint $1 cd "$_BASE"
}
# cd / to goto root
cd_root_rw() {
cd "$_BASE"
}
cd_root_ff() {
cd /
}
cd_root_prologue() {
cat <<-:
The last special directory you should know about is the "$(bld root)"
directory. The name of the root directory is a single slash $(path /).
$(path /) is the ultimate parent of every directory on a Unix system. The root
directory is the only directory on a Unix system that has no parent.
(Technically, $(path /) is $(bld its own) parent! Good luck figuring that out...)
If you run $(cmd cd ..) from $(path /), you don't actually go anywhere.
Go to the root directory $(path /).
:
}
cd_root_test() {
if [[ "$PWD" = / ]]; then return 0
elif _tutr_noop; then return $NOOP
else _tutr_generic_test -c cd -a / -d "/"
fi
}
cd_root_hint() {
_tutr_generic_hint $1 cd "/"
[[ $1 = $WRONG_PWD ]] && return
cat <<-:
The name of the root directory is a single front slash $(path /).
This command will take you to the root directory:
$(cmd cd /)
:
}
cd_root_epilogue() {
cat <<-:
The root directory gets its name by analogy. If you consider the
hierarchy of subdirectories to be a tree, then the root directory is at
the bottom.
:
if [[ -d /root ]]; then
cat <<-:
If you run $(cmd ls) from here you will see a directory down here named
$(path root). This directory is $(bld NOT) the same thing as $(bld the root)
directory $(path /). "$(path root)" is actually the home directory of the user
account named "$(bld root)".
For historical reasons, $(bld root) is the name of the administrator account on
Unix. $(bld root) is all-powerful and able to do $(bld anything) on that system.
$(bld Root) user, $(bld root) directory... yeah, it is kinda confusing.
:
fi
cat <<-:
Feel free to use $(cmd ls) to look around while you are down here.
:
_tutr_pressenter
}
# cd - get back to lesson
cd_minus1_rw() {
cd /
}
cd_minus1_ff() {
cd "$_BASE"
}
cd_minus1_prologue() {
if [[ $_BASE != $OLDPWD ]]; then
cat <<-:
Since you've used $(cmd cd) to go into another directory, using $(cmd cd -) to
return to the previous directory won't take you back to where you
need to go. In this case, run
$(cmd 'cd $_BASE')
to return to the lesson.
:
else
cat <<-:
When you are ready, use $(cmd cd -) to continue the lesson.
:
fi
}
cd_minus1_test() {
if [[ "$PWD" = "$_BASE" ]]; then return 0
elif _tutr_noop; then return $NOOP
else _tutr_generic_test -c cd -a - -d "$_BASE"
fi
}
cd_minus1_hint() {
_tutr_generic_hint $1 cd "$_BASE"
}
cd_a_file_pre() {
[[ -d $_BASE/a_file ]] && rm -rf $_BASE/a_file
! [[ -f $_BASE/a_file ]] && touch $_BASE/a_file
}
cd_a_file_prologue() {
cat <<-:
Now it's time for some $(_err error messages). Yay!!!
What happens if you give $(cmd cd) an argument that is not a directory?
There is a file here called $(path a_file). Try to $(cmd cd) into this file to see
what happens.
:
}
cd_a_file_test() {
if ! [[ -f "$_BASE/a_file" ]]; then
touch "$_BASE/a_file"
return 99
elif [[ -d "$BASE/a_file" ]]; then
rm -rf "$BASE/a_file"
touch "$BASE/a_file"
return 98
elif [[ ( ${_CMD[0]} = cd || ${_CMD[0]} = pushd || ${_CMD[0]} = popd ) \
&& ${_CMD[1]} = a_file \
&& "$PWD" = "$_BASE" \
&& $_RES == 1 ]]; then return 0
elif _tutr_noop; then return $NOOP
elif [[ -n $ZSH_NAME ]]; then _tutr_generic_test -f -c cd -a a_file -d "$_BASE"
# cd'ing into a file returns success in Bash
else _tutr_generic_test -c cd -a a_file -d "$_BASE"
fi
}
cd_a_file_hint() {
case $1 in
99)
cat <<-:
Whoops! Somehow that file disappeared.
I just replaced it for you so you can try again.
Now run
$(cmd cd a_file)
:
;;
98)
cat <<-:
That was weird; somehow the file $(path a_file) was actually a directory.
I just replaced it for you so you can try again.
Now run
$(cmd cd a_file)
:
;;
*)
_tutr_generic_hint $1 cd "$_BASE"
cat <<-:
Then run
$(cmd cd a_file)
to see what happens.
:
;;
esac
}
cd_a_file_epilogue() {
_tutr_pressenter
cat <<-:
That wasn't so bad, was it?
:
_tutr_pressenter
}
cd_a_file_post() {
_RES=0
}
# cd not_a_dir # fails
cd_not_a_dir_pre() {
[[ -d $_BASE/not-a-dir ]] && rm -rf $_BASE/not-a-dir
_AGAIN=
_TIMES=0
}
cd_not_a_dir_prologue() {
cat <<-:
There is one last thing to try with the $(cmd cd) command: what if you try to
$(cmd cd) into a directory that does not exist?
There is no directory here called $(path not-a-dir) (I made sure).
Try to use $(cmd cd) to enter a directory by that name to see what happens.
:
}
cd_not_a_dir_test() {
_NOT_A_DIR_EXISTS=99
if [[ -a $_BASE/not-a-dir ]]; then
rm -rf $_BASE/not-a-dir
(( _TIMES++ > 0 )) && _AGAIN=" (again)"
return $_NOT_A_DIR_EXISTS
elif [[ ( ${_CMD[0]} = cd || ${_CMD[0]} = pushd || ${_CMD[0]} = popd ) \
&& ${_CMD[1]} = not-a-dir \
&& "$PWD" = "$_BASE" \
&& $_RES == 1 ]]; then return 0
elif _tutr_noop; then return $NOOP
elif [[ -n $ZSH_NAME ]]; then _tutr_generic_test -f -c cd -a not-a-dir -d "$_BASE"
# cd'ing into a non-existent file returns success in Bash
else _tutr_generic_test -c cd -a not-a-dir -d "$_BASE"
fi
}
cd_not_a_dir_hint() {
case $1 in
$_NOT_A_DIR_EXISTS)
cat <<-:
Are you trolling me? $(path not-a-dir) is not supposed to exist.
That's the whole gimmick of this step.
$(red Sigh) I removed it$_AGAIN so you can do this the right way.
Please run
$(cmd cd not-a-dir)
so we can get on with things.
:
;;
*)
_tutr_generic_hint $1 cd "$_BASE"
[[ $1 = $WRONG_PWD ]] && return
cat <<-: