LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 01-06-2015, 01:08 AM   #1
Needsashave
LQ Newbie
 
Registered: Apr 2013
Posts: 6

Rep: Reputation: Disabled
mkdir not working even with the -p flag whilst inside a loop & with variable name


Code:
#!/bin/bash
for file in *.txt;

do
        awk -F, '{print $3}' $file | cut -d "'" -f 4 2>&1 $country;
        mkdir -p "$country";
        #cp $file ./$country;
done
Gives me the following error message;
Quote:
US
mkdir: cannot create directory `': No such file or directory
AU
mkdir: cannot create directory `': No such file or directory
AU
mkdir: cannot create directory `': No such file or directory
and so on.

It's taken me about two hours to assemble this code, and even though it doesn't work, i'm still proud of it
 
Old 01-06-2015, 01:26 AM   #2
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and Scientific Linux
Posts: 5,753

Rep: Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288
Hi,

the variable "$country" contains nothing. You can check this with an echo statement. To assigne the output of your awk construction into that variable you can do something like:

Code:
#!/bin/bash
for file in *.txt; do
  country="$(awk -F, '{print $3}' $file | cut -d "'" -f 4)"
  mkdir -p "$country"
  #cp $file ./$country
done
HTH,

Evo2.
 
1 members found this post helpful.
Old 01-06-2015, 04:46 AM   #3
Needsashave
LQ Newbie
 
Registered: Apr 2013
Posts: 6

Original Poster
Rep: Reputation: Disabled
Worked a treat, thanks heaps.
I'd tried to populate $country that way, but none of my syntax worked. Obviously my redirect didn't either.
Anyway, you've made my day.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
cat command inside foreach loop not working Praveen waliitagi Linux - Newbie 6 10-17-2013 02:09 AM
how to print a variable inside while, outside of the loop. basusimply Programming 7 09-18-2013 03:22 PM
Sleep inside for loop inside while loop causing issues. TheOnlyQ Programming 13 12-19-2012 12:59 PM
[SOLVED] exporting a variable defined inside a for loop musonio Linux - General 6 11-06-2009 08:10 PM


All times are GMT -5. The time now is 02:11 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration