Solved

Bash Variable from Command Line

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

ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
"netsh firewall" or "netsh advfirewall firewall" 10 86
improve Shell script 5 150
RoboCopy to Changing External Drives 2 44
Shell Script on AIX 7 96
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 …
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 …
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…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

770 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