Solved

Bash Variable from Command Line

Posted on 2014-11-13
3
155 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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Utilizing an array to gracefully append to a list of EmailAddresses
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
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…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

757 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

20 Experts available now in Live!

Get 1:1 Help Now