?
Solved

Bash Variable from Command Line

Posted on 2014-11-13
3
Medium Priority
?
165 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 19

Accepted Solution

by:
simon3270 earned 2000 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Over the years I've spent many an hour playing on hardened, DMZ'd servers, with only a sub-set of the usual GNU toy's to keep me company; frequently I've needed to save and send log or data extracts from these server back to my PC, or to others, and…
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…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Suggested Courses

741 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