Stop learning alone!

Learn faster and stay on-track by joining this free class with other self-learners.

Register for Bash Scripting now.

Bash Scripting

Class length: 18 weeks. Start anytime.

Creator: kday

Status: Established

Join this class!

Lesson 1: 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

9 total

rohshall (Self-grade: Pretty good)
Submitted 4 months ago | Permalink | Time spent: 30 minutes
ahunsaker (Self-grade: Outstanding)
Submitted 9 months ago | Permalink | Time spent: 30 minutes

The discussion section on braces and parentheses was something I have over looked in the past, now its something to add to my bash skillZ.

I ran into the same thing with the tail +2 command. Unless you setup the _POSIX2_VERSION variable, the command didn't work on RHEL 5.6 but would work with on Solaris 10 x86

swedishjeff (Self-grade: Pretty good)
Submitted 9 months ago | Permalink | Time spent: 29 minutes

Was told that although >& will work (in Bash), the old school 2>&1 is better for compatibility. I do like the >& shortcut though.

I never knew about putting a 'tee' in a named pipe before either, that was nice.

I have already started modifying my scripts to use the $( ) instead of backticks. Much better, and easier to read.

Overall some good tips and a little info I might never use (noclobber?). But it's handy to have available.

mgualtieri (Self-grade: Pretty good)
Submitted 1 year ago | Permalink

I'm late to the party!

I've been doing shell scripting for a while now, but I learned a lot of stuff from this chapter -

for instance, the fact that { } and also ( ) will group commands together so you can redirect to a single file - the braces have a few more quirks though, with the whitespace requirement and the trailing semicolon. That will come in useful in some scripts I'm writing.

Also, I was always confused by the $( ) as a substitute for backticks. I prefer the look of $( ) to the backticks, but I understand that the backticks provide more backwards compatibility.

'tee' is awesome for logging... I use tee -a to append to logfiles when I want both stdout and stderr redirect, while still showing output on the screen. Helpful when you're sitting in front of a terminal running a script but you still want logging so you don't have to put a thousand 'sleep' commands in there so you can catch up ;-)

Comments:

kday
1 year ago

Thanks for joining the class. Don't worry about being "late." Whenever people can start the class is just fine. I'm glad that you're learning a lot already.

Sign up or log in to comment

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

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 ``

sundaryourfriend (Self-grade: Pretty good)
Submitted 1 year ago | Permalink

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.

johnny (Self-grade: Outstanding)
Submitted 2 years ago | Permalink

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

Comments:

kday
2 years ago

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

Sign up or log in to comment

zlu (Self-grade: Pretty good)
Submitted 2 years ago | Permalink

i skipped 2.18 but did the rest of the questions.

Comments:

kday
2 years ago

Nice job!

Sign up or log in to comment

kday (Self-grade: Pretty good)
Submitted 2 years ago | Permalink | Time spent: 30 minutes

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

Comments:

zlu
2 years ago

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

pbr
2 years ago

Try this:

tail --lines=+2
kday
2 years ago

tail /tmp/ls.out worked fine and 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
2 years ago

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

kday
2 years 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
2 years 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
2 years 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.

mgualtieri
1 year ago

This is probably old news, but I figured I'd throw my $0.02 in... at press time this probably wasn't a deprecated option, but it appears to have been phased out a few years ago. Now you'd need to set the variable _POSIX2_VERSION=199209 if for some reason you wanted to do that, like below:

[root@atlas ~]# export _POSIX2_VERSION=199209
[root@atlas ~]# 
[root@atlas ~]# tail +2 foo.lines 
line 2
line 3
line 4
line 5
line 6
line 7
kday
1 year ago

Cool, thanks for the clarification. It's good to know that it wasn't something that I was doing wrong. Nice work digging that up.

Sign up or log in to comment