[SOLVED] Question about a bash script creating a folder variable
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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
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.
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.