View Full Version : shell script help?
elfstone214
June 6th, 2007, 02:57 AM
hey guys,
can someone help me do the following?
I need to read in the first and only line in myfile.txt which will be something like "case1" or "case2", etc. Then I need to create a directory with the name of that string which was read in (ie. a folder called case1).
I can read the line from the file using:
grep "case" myfile.txt
but I can't figure out how to use that and make the folder.
Thanks!
Stephen Howard
June 6th, 2007, 03:27 AM
Sounds like a homework assignment!
Jergar
June 6th, 2007, 03:45 AM
How about
name=`grep case file.txt`
mkdir $name
regards
Jergar
elfstone214
June 6th, 2007, 12:16 PM
Sounds like a homework assignment!
it's amazing how many times you can ask a question on this topic and get this answer ... it gets really old.
Anyway it is for part of one of my thesis codes. I need to randomize the folder which contains the input files because I will be using a quadcore processor and it may end up uploading muliple sets of input files (different cases) at the same time which will override the other.
elfstone214
June 6th, 2007, 12:24 PM
How about
name=`grep case file.txt`
mkdir $name
regards
Jergar
Jergar,
thanks but I tried that you said and it created a directory called grep, and another called case
meatpan
June 6th, 2007, 02:08 PM
Jergar,
thanks but I tried that you said and it created a directory called grep, and another called case
It sounds like you used quotes instead of backticks.
Quote: '
backtick: `
The difference is subtle, which leads to some frustrating misunderstandings when reading or copying a script. The backticks are instructions to the shell to run a command in a sub-shell. The output of the sub-shell command is returned and can be assigned to a variable. In this case, the variable name was assigned the output of : grep case file.txt
This type of backtick substitution is useful, and used fairly often in shell scripting. Be aware that grep returns nothing if there is no match, and multiple values if there are multiple matches. Because of that, consider:
name=`head -n 1 file.txt`
if [ -z $name ]; then
mkdir $name
fi
This block of code will read the first line of the file, and then check whether or not name recieved an assignment.
bashologist
June 6th, 2007, 02:18 PM
It sounds like you used quotes instead of backticks.
name=`head -n 1 file.txt`
if [ -z $name ]; then
mkdir $name
fi
"-z" is zero length, "-n" is nonzero. I think you wanted "-n" here. Also, you forget to quote your variable in that test. And here's my solution to the backtick confusion:
man test
http://wooledge.org/mywiki/BashFAQ#faq82
elfstone214
June 6th, 2007, 03:35 PM
thanks guys!
you were right I was using quote instead of backstick ... I have fallen for this more than once already #-o
mortenkjeldgaard
June 10th, 2007, 05:04 PM
you were right I was using quote instead of backstick ... I have fallen for this more than once already #-o
Yes backticks are a pain. Sometimes they are on dead keys. Fortunately, bash provides a special variable to do the same:
name=$(grep case text.txt)
I find that more readable than backticks.
Cheers,
Morten
vBulletin® v3.8.7, Copyright ©2000-2012, vBulletin Solutions, Inc.