Bash Scripting

Class length: 18 weeks. Start anytime.

Creator: kday

Status: Established

Assignment 1: Problem set 1

Do all of the examples throughout Chapter 2 in Bash Cookbook by O'Reilly.

To submit the homework, just leave a comment saying that you completed it and mention any problems you had or any cool things you learned. Don't print out your whole session history, it gets really long really fast. Do post code specific examples that you want to discuss.

If you copy and paste any of your bash history, make sure that you don't post anything sensitive. Create dummy directories for working with files, don't post any passwords, etc.

The assignment should take about 45 minutes to an hour to complete.

Bash Tip: If you want to stop a command cleanly, type 'Ctrl + C'.

Homework Submissions (5 total):

kday (Self-grade: Pretty good)
Submitted 6 months ago

I couldn't get 2.12 to work, Skipping a Header in a File. tail kept trying to open the file '+2' and gave an error (see below).

I also skipped a couple towards the end like 2.19 and 2.20. The rest of the examples worked well, and I learned more than I expected to given it's the first chapter.

kevin@heater:~/bashtest2$ tail +2 /tmp/ls.out
tail: cannot open `+2' for reading: No such file or directory
==> /tmp/ls.out <==
k
s
a
d
f
g
h
j
k
s
kevin@heater:~/bashtest2$ tail /tmp/ls.out +2
==> /tmp/ls.out <==
k
s
a
d
f
g
h
j
k
s
tail: cannot open `+2' for reading: No such file or directory
Permalink

Comments:

zlu
6 months ago

what happens when you do: tail /tmp/ls.out

pbr
6 months ago

Try this:

tail --lines=+2
kday
6 months ago

tail /tmp/ls.out worked fine a produced the list that was displayed twice.

pbr's suggestion worked. Thanks! Here's my output for a /tmp/ls.out with slighly different content:

kevin@heater:~/bashtest2$ tail /tmp/ls.out
a
d
f
g
h
j
k
s
kevin@heater:~/bashtest2$ tail --lines=+2 /tmp/ls.out
d
f
g
h
j
k
s
kevin@heater:~/bashtest2$ tail --lines=+3 /tmp/ls.out
f
g
h
j
k
s
kevin@heater:~/bashtest2$ tail --lines=+4 /tmp/ls.out
g
h
j
k
s
zlu
6 months ago

i can't understand 2.18. could someone upload an example of what the 'divert' script may look like?

kday
6 months ago

Sorry, I can't help on that one. I skipped it too. I figured if I had a script with multiple types of output I can come back to that example. I haven't faced that kind of scenario yet though.

johnny
6 months ago

to expand on pbr's comment, this is from the man page:

   -n, --lines=N
          output the last N lines, instead of the last 10; or  use  +N  to
          output lines starting with the Nth

so either --lines or -n will work.

kday
6 months ago

Thanks, johnny, that makes sense. For some reason the book says that if you use a + sign then it has the same effect as -n or --lines. Apparently didn't work for me.

Sign up or log in to comment

zlu (Self-grade: Pretty good)
Submitted 6 months ago

i skipped 2.18 but did the rest of the questions.

Permalink

Comments:

kday
6 months ago

Nice job!

Sign up or log in to comment

johnny (Self-grade: Outstanding)
Submitted 6 months ago

All done... (better late then never). The crazy file redirection was new to me e.g. 3>&1 1>&2 2>&3.

Permalink

Comments:

kday
6 months ago

Cool. No problem on the lateness. Glad you're able to join the action.

Sign up or log in to comment

sundaryourfriend (Self-grade: Pretty good)
Submitted 4 months ago

I read only whatever the O'Reilly preview allowed - which is not very less since only a few pages seem to have been skipped. In particular, I could not read 2.4,5,10,11,12,17,18,22 parts. However, I can guess they are about "echo -n", ">>", head, tail, xargs, and ">!". Am I correct? :)

The 'type' command and the swapping of STDOUT and STDERR were new and very useful to me.

I'm planning to get an ebook version of this book soon.

Permalink
bhrgunatha (Self-grade: Pretty good)
Submitted 1 month ago

These are the things I learned that surprised me

ls formatting depends on whether its output is going to the terminal or a file.

contents 1> out 2> err
contents > out 2> err
contents >& out+err
contents >> appended

() runs in a subshell

using pipes causes commands to run in subshells

tee duplicates output : ... uniq | tee /tmp/x.x | awk -f transform.awk ...

$() uses output of subshell as a string

e.g. rm $(find . -name '*.rb')

equivalent to backticks ``

Permalink

Recent Class Activity

BTheMad joined Bash Scripting
1 week ago
bhrgunatha submitted Lesson 3 HW 1
3 weeks ago
rch joined Bash Scripting
1 month ago
kday commented on johnny's homework
1 month ago
bhrgunatha submitted Lesson 2 HW 1
1 month ago
bhrgunatha commented on johnny's homework
1 month ago
kday submitted Lesson 8 HW 1
1 month ago
adaptives joined Bash Scripting
1 month ago

Class Members (73)

BTheMad
Joined 1 week ago
rch
Joined 1 month ago
adaptives
Joined 1 month ago
bhrgunatha
Joined 1 month ago
kksm19820117
Joined 1 month ago
kevinelliott
Joined 2 months ago
JessicaParkers
Joined 3 months ago
tammyschwartzerj
Joined 3 months ago

All members


License

Attribution Non-Commercial Share Alike