0

This is for our weekly prac, no marking or grade (we don't even get the answers!)

I'd really appreciate it if anyone has the time to help explain this exercise to me, as I'm trying to get this work done over the weekend and won't be able to get assistance from my tutor until next week. I've been doing a lot of reading and work covering bash programming, regex and grep. But the code below has just got me flummoxed.

I'm even wondering, are the instructions incomplete or am I just not getting it?

Specifically the following are preventing me from understanding the entire thing:

NAMES=`ls *.$1`

filed=$(file:$1).$2

Exercise 4.3
Suppose you are given a text file (called myfile) containing the following

#!/bin/bash

NAMES=`ls *.$1`

for file in $NAMES; do
    filed=$(file%:$1).$2
    mv file filed
done

Execute the following commands $ grep ' file ' myfile

 $ grep 'file*' myfile

 $ grep '^ *file*' myfile

 $ grep 'file$' myfile

 $ sed 's/ file* / script /g' myfile

 $ sed ' /for/,/files/d' myfile

 $ sed '/\$[A-Z]..[A-Z]*/d' myfile

Then explain the output or results

  • I should add, I'm fine with the grep and sed commands, it's the bash script that I don't understand – user65751 May 10 '14 at 13:04
1
NAMES=`ls *.$1`

This is an assignment. The variable $NAMES is assigned a value. The value is taken as the output of the command in backquotes (see Command Substitution in man bash). $1 is a special parameter, it corresponds to the first argument of the script. So, it seems the script should be run with an extension as a parameter (e.g. ./script.sh txt), output of the ls command will be stored in the variable.

filed=$(file%:$1).$2

This is really strange. Are you sure the parentheses weren't curly brackets, in fact? And there also should be a . instead of the :.

filed=${file%.$1}.$2

${var%pattern} cuts the pattern from the end of the variable. See Parameter Expansion in man bash for details. So, in this case, it strips the dot plus the extension given by the first parameter and adds a dot and the second parameter.

BTW, the move line should probably be

mv $file $filed

otherwise there would be no reason to use a loop.

  • I was really thrown by the : too. I'd only seen it used as a delimiter so far. Yeah, do you think NAMES is meant to be an array? It would seem like it, from the fact it is called NAMES, plus the the loop. But the syntax is wrong, as you say, needs curly braces. I think the file%$1.$2 is to remove the suffix, then concatenate param $1 and param $2 to the filename. It just occurred to me that I have seen a programme for creating a backup of a directory which was similar to this. I'm going to see if I can find it, might shed some light on what this is meant to be doing – user65751 May 10 '14 at 22:49
  • @user65751: NAMES is not an array here, just a string. When used without double quotes, it gets split to words in for f in $NAMES. – choroba May 10 '14 at 22:52
  • oh right. So are the $1 and $2 referring to? And are the curly brackets being used for concatenation? – user65751 May 10 '14 at 22:57
  • @user65751: $1 and $2 are the first and second argument of the script. Curly brackets are used for parameter expansion. Have you read the passages in man bash? – choroba May 11 '14 at 15:38
  • thanks, I'm already familiar with command substitution.I've just spent the last 2 weeks reading the chapters in A practical Guide to Linux Commands, Editors and Shell Programming, plus the whole beginners, and some of the advanced bash scripting guide from TLDP, and man pages. I had trouble understanding the ls command, I couldn't visualise what NAMES would look like. And then the filed assignment, it just combined things in a ways that I couldn't work out, plus the : Thanks so much for you help! – user65751 May 12 '14 at 9:29

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.