LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-16-2012, 08:28 PM   #1
TwoPresidents
LQ Newbie
 
Registered: Apr 2012
Posts: 3

Rep: Reputation: Disabled
Question about a bash script creating a folder variable


I'm trying to write a script where you create a folder ( different name each time), then you CD into the folder, and then vim starts automatically. Something like this --

Code:
mkdir <example> && cd <example> && vim <filename>

Variables don't work the same way in DOS (the %1), and what I have below doesn't quite work (I'm using CYGWIN and creating directories in windows).


Code:
#!/bin/bash
dir=$dir
mkdir $dir && cd $dir && vim filename.txt
It's technically creating the folder, but it's adding some mysterious dot next to the filename in windows, and when deleted it shows "removed foldername/r". I don't know if that's just how it works, of if I'm doing something wrong. Anything would be appreciated, thank you.
 
Old 04-16-2012, 09:43 PM   #2
fredora
LQ Newbie
 
Registered: Nov 2010
Posts: 19

Rep: Reputation: 1
Script

I have no access to test this now but here's a swing at it.

Code:
#!/bin/bash


echo "Enter Directory Name: "
read dir

echo "Enter Filename: "
read file

sudo mkdir $dir
vim $file
Assuming you create that in the directory you want to execute it, you shouldnt need to 'cd' to it.
Double check you have r+x permissions on the file
then execute -> ./scriptname.sh

Let me know if that works.
 
Old 04-16-2012, 10:02 PM   #3
TwoPresidents
LQ Newbie
 
Registered: Apr 2012
Posts: 3

Original Poster
Rep: Reputation: Disabled
Here's the result. I swear if I can get that "/r" out of the way, then I'm good to go. I don't know where it's coming from. No clue. Perhaps CYGWIN is temperamental. Thanks for giving it a shot though, this seems so basic, I shouldn't even be asking it. Haha.

Code:
$ ./scriptname.sh
./scriptname.sh: line 2: $'\r': command not found
Enter Directory Name:
Dir
': not a valid identifierread: `dir
./scriptname.sh: line 5: $'\r': command not found
Enter Filename:
Filename.txt
': not a valid identifierread: `file
./scriptname.sh: line 8: $'\r': command not found
For the hell of it, I tried running it in linux. Didn't work there either, says permission denied, but I'm SU and it still didn't work. Weird. I can both make directories though, and can also create a file, however it doesn't work through the script. Haha. Fun.
 
Old 04-16-2012, 10:25 PM   #4
TwoPresidents
LQ Newbie
 
Registered: Apr 2012
Posts: 3

Original Poster
Rep: Reputation: Disabled
[Solved]

I figured it out, for anyone that somehow ends up here.

Fredora's script worked, but you have to use d2u first, which is a program that converts characters from DOS to UNIX (even though you're using a CYGWIN environment in Windows). The correct syntax is below.

Code:
d2u ./<filename>
After you run that, your command should work, presuming the actual code is right. Thanks for the help fredora.
 
Old 04-17-2012, 10:11 AM   #5
fredora
LQ Newbie
 
Registered: Nov 2010
Posts: 19

Rep: Reputation: 1
Under thread tools at the top, you can mark the thread as solved
 
Old 04-19-2012, 06:55 PM   #6
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
Quote:
Originally Posted by TwoPresidents View Post
I figured it out, for anyone that somehow ends up here.

Fredora's script worked, but you have to use d2u first, which is a program that converts characters from DOS to UNIX (even though you're using a CYGWIN environment in Windows). The correct syntax is below.

Code:
d2u ./<filename>
After you run that, your command should work, presuming the actual code is right. Thanks for the help fredora.
Better still, just create the file with a Cygwin editor, like vi/vim. Then there won't be any superfluous characters there to begin with.
--- rod.
 
  


Reply



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
[SOLVED] Bash question about File Manager creating ( untitled \folder ) theKbStockpiler Linux - Newbie 3 07-22-2011 07:01 PM
[SOLVED] Bash script question, searching in a variable, displaying search results... Wolfjurgen Programming 5 04-26-2011 12:29 PM
question regarding bash script variable mrmnemo Linux - Desktop 4 03-22-2010 12:55 PM
Simple variable bash script help/question lilrazzzzz Programming 4 04-29-2009 05:50 PM
Urgent : creating folder using bash script prernasin Linux - Newbie 2 09-26-2007 01:58 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:45 AM.

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
Open Source Consulting | Domain Registration