Solved

Bash Variable from Command Line

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
busybox bash/ash - bash -x | grep 19 207
get remote physical servers and platforms 5 59
Call Shell Script from Perl Script 6 195
Bash Script to Analyze Oracle Schemas 11 128
Background Still having to process all these year-end "csv" files received from all these sources (including Government entities), sometimes we have the need to examine the contents due to data error, etc... As a "Unix" shop, our only readily …
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
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…

739 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