Solved

Bash Variable from Command Line

Posted on 2014-11-13
3
157 Views
Last Modified: 2014-11-14
I am having trouble populating a variable with a command line. I am sure I am formatting it incorrectly, but I am not sure how. I want to take a list of URLs, something like:

http://blah.com/test
http://blah.com/test2

and print out:
test
test2

I am using the following, but getting an error "./test.sh: line 3: =: command not found"

#!/bin/bash
while read LINE; do
   $NAME = `echo $LINE | cut -d \/ -f4`
   echo $NAME
done < url-list.txt

Any ideas on how I formatted the $NAME variable wrong?
0
Comment
Question by:stakor
  • 2
3 Comments
 
LVL 19

Accepted Solution

by:
simon3270 earned 500 total points
ID: 40442025
You mustn't put the $ at the start when setting the value of NAME, and you mustn't have spaces round the = sign, so

   NAME=`echo $LINE | cut -d \/ -f4`
0
 
LVL 19

Expert Comment

by:simon3270
ID: 40442035
There are a couple of other things to consider. Firstly you don't need the backslash in front of the forward slash - you only need to do that if / is a "special character" in the location you are using it, i.e. one which has a particular effect.  For the delimiter in "cut", "/" is just a normal character like others, so just use "-d / -f4".  It isn't actually an error here to use the backslash, but it makes the code harder to read, and it may cause problems if the command gets more complex.

The other is the format of your input lines. Will they always be like that, with just one name after the domain, or might they be longer, such as http://example.com/images/test3 - in that case, do you want to print "image", or "test3", or "image/test3" - all are possible!
0
 

Author Closing Comment

by:stakor
ID: 40442798
Awesome, thank you.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This Windows batch file is useful for organizing image files from a digital camera or other source, but can have many other uses.  It simply renames the file(s) to match their create date.  For example, if you took a picture today at 1:40pm and the …
Recently, an awarded photographer, Selina De Maeyer (http://www.selinademaeyer.com/), completed a photo shoot of a beautiful event (http://www.sintjacobantwerpen.be/verslag-en-fotoreportage-van-de-sacramentsprocessie-door-antwerpen#thumbnails) in An…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

948 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now