Passing nano a specific filename inside a function - function

I'm relatively new to shell in general, only recently started learning bash scripting and how to use it in automation. Problem is that I use zsh as my daily shell and would like to get into habit of using command line text editor and specifically nano, at least for now.
To do so (and also to practice making scripts and tweaking configuration files) I tried to make a function within .zshrc which would cd me into a particular directory I use for my Free Writing library, then create a file with today's date as its name and enter interactive nano session.
Code of said function
function ok_go() {
cd ~/Library/Mobile\ Documents/com~apple~CloudDocs/Freewriting
nano $(date %d.%m.%Y)
}
Problem is nano doesn't really get the name, it cd's into the right directory, starts a file editor but the name is blank. But for some reason, if I use nano $(date %d.%m.%Y) interactively (call it myself) it works as I intended, i.e start the edit of the file with a name of something like 21.04.2021.txt
Don't really get why it doesn't work from a function call and how can I make it behave like a want. Thanks!

Related

How can I duplicate a folder and change the name?

I'm trying to setup a signup form on my webpage, once the user creates there account, I would like to automatically create a folder of what the user typed in a input with the ID of cfinput, the problem i'm running into, is I am not experienced in the jQuery zones, I focus on creating front-end design using CSS and HTML languages. Summarization, I can't figure out how to create a copy of a folder named base_org and change it's name to what the user wrote in the input listed above,
Any help is greatly appreciated, if you have any questions just comment and I will try to re-edit my post to make it more clear.
JavaScript cannot do this on the client side. You need a backend which processes the signup form submission and creates a copy of your base directory.
A general solution is to call the system's copy command. On Linux, you can copy a directory like this:
cp -r /path/to/base_org /path/to/new_folder
You can execute the shell command in your preferred backend language. For example in PHP, you can use shell_exec:
// process the form first and put username in $username
shell_exec("cp -r /path/to/base_org /path/to/$username");
Make sure the username won't contain any invalid or special characters or other shell commands.
Another option would be to use the backend language's API to copy the directory.

First time coding and confused - Echo

Apologies for incredible ignorance. First time ever looking at or trying coding in any form and all naturally a bit confusing and overwhelming.
Trying to keep it super basic I'm attempting to build something basic for Amazon Echo by working through this article - https://developer.amazon.com/blogs/post/Tx3DVGG0K0TPUGQ/updated-alexa-skills-kit-fact-template-step-by-step-guide-to-build-a-fact-skill
Have got to Step 2.3
Once you have the source downloaded [done], node installed and npm updated, you are ready to install the ASK-SDK. Install this in the same directory as your src/index.js file for your skill. Change the directory to the src directory of your skill, and then in the command line, type: npm install --save alexa-sdk
I've moved the SDK into the same folder as the source - in downloads folder. Am confused on changing the directory to the same as my skill. As far as I know there is no skill yet so not sure where to move it to.
When type in npm install --save alexa-sdk
returns
npm WARN enoent ENOENT: no such file or directory, open '/Users/OwenLee/package.json'
npm WARN OwenLee No description
npm WARN OwenLee No repository field.
npm WARN OwenLee No README data
npm WARN OwenLee No license field.
working on a mac so don't really know how/where to access this, but assuming this is where i need to move the files to?
Very sorry for baby-basic knowledge. Just trying to at least get a foot in the door as know need to learn this stuff but everything i read seems to assume i already have a working knowledge of coding : S
any help would be awesome - inc. any advice on steps after that you can probably see i'll trip up on
thanks!!
oven121
So as far as the directory /Users/OwenLee/ this would be your home folder on a Mac. The Root / of your HDD can be reached through Finder by clicking on Macintosh HD (or whatever you named your main hard drive) in the side bar. If you open up a new Terminal window it will be the directory that the terminal starts in. You should be able to fix your problem by taking the file packages.json, which should be wherever you downloaded the SDK to, and placing it in your home folder, then re-running the command.
Now don't let me change your mind if you're truly committed, but if you have absolutely no experience with programming I would recommend starting with something a bit simpler than Java or Javascript. Object oriented languages can be both very convoluted and difficult to get the hang of for beginners (I personally have been writing native languages like C for years and am just now starting to understand how Java works.).
If it is an option I recommend starting with a language that your Mac has built in support for. Perhaps start with Bash scripting or Apple Script making basic scripts to do things you find tedious to do manually in a terminal, or get to know the basics of processor-native languages like C & C++ by making some basic programmes to display text when it is run, or to ask the user to type something, and say back what they typed. Finally since you are on a Mac you can get Xcode for free in the app store, it will configure itself and you could play around with it to learn how macOS handles windows, perhaps start by making a basic programme window with a few buttons that do different things when clicked.
If you have any interest in my suggestions you can find some info about bash scripting here: https://linuxconfig.org/bash-scripting-tutorial the tutorial says it assumes the reader has no previous knowledge of Bash, and most commands should work fine in the version of Bash built into your Mac's Terminal app.
If you take more interest in C++ this is the site that I used to learn to write it, and learn how native languages work: http://www.cplusplus.com/doc/tutorial/
Finally here is a basic C++ programme called "Hello World", it is somewhat of an initiation rite of C/C++ students to write this programme and learn how each part of it works:
//HelloWorld.cpp the double slash tells the compiler and user that everything after it on this line is a comment, not code//
#include <iostream> //The octothorp '#' lets the compiler know it needs to use the library named inside the pointed brackets '</>' when it builds the programme. 'iostream' stands for In-Out Stream, and handles basic text, and basic processor commands//
using namespace std; //This line tells the compiler that any line that says to show text or ask the user to type something should use regular text and not a special format//
int main() //'int' stands for integer, any time you make a variable that contains only an integer you should put this in front of it's name, and 'main' is the name of the integer. The empty parentheses tells the compiler that this is a function, rather than a number//
{ //The open curly bracket '{' tells the compiler where the function starts
cout<<"Hello World"; //'cout' stands for 'character out' and is for showing basic text in the terminal window. The double pointy 'out' brackets '<<' tells the compiler that the text should be sent out of the programme rather than loaded into a variable, the text inside the quotes is what will be shown on the screen, and the semi colon tells the compiler where the command ends, it has to be put at the end of any command that is inside of a function//
return 0 //The command 'return' is for telling the compiler whether or not an error has occurred, 0 means the programme ran fine, 1 means something went wrong, either way the programme closes when it runs the command 'return'//
} //the closed curly bracket tells the compiler where the function ends//
Good luck with your programming, and if you have any questions unrelated to this thread please feel free to private message me, or create a new question and tag me in it so that I get notified.

Post build event command whose directory is not C:\

I want to run a command in the post build event of a project, if i do C:\temp\Client.exe it works fine but If my project is in the thumb drive and change the command to G:\temp\Client.exe it looks like it doesnt like the path and exit with code 9009. what command do i need if the exe is in different path than C drive. Thankx
From the question and subsequent comment it looks that drive letter keeps changing. It is possible in post-build section check if the file exist before call it.
For debugging you can use something like
f:
Dir
And view output window after the build.

Solution to programmatically generate an export script from a directory hierarchy

I often have the following scenario: in order to reproduce a bug for reporting, I create a small sample project, sometimes a maven multi module project. So there may be a hierarchy of directories and it will usually contain a few small text files. Standard procedure would of course be to create a zip file and send that. But on some mailing lists attachments are not allowed, and so I am looking for a way to automatically create an installation script, that I can post to such mailing lists.
Basically I would be happy with a Unix flavor only that creates mkdir statements to create directories and >> statements to write the file contents. (Actually, apart from the relative path delimiters, the windows and unix versions can probably be identical)
Does such a tool exist somewhere? If not, I'll probably write one in java, but I'm happy to accept solutions in all kinds of languages.
(The tool could run under windows or unix, but the target platform for the generated scripts should be either unix or configurable)
I think you're looking for shar, which creates a shell archive (shell script that when run produces a given directory hierarchy). It is available on most systems; you can use GNU sharutils if you don't already have it.
Normal usage for packing up a directory tree would be something like:
shar `find somedirectory -print` > archive.sh
If you're using GNU sharutils, and want to create "vanilla" archives which use only the most portable of shell builtins, mkdir, and sed, then you should invoke it as shar -V. You can remove some more extra baggage from the scripts by using -xQ; -x to remove checks for existing files, and -Q to remove verbose output from the archive.
shar -VxQ `find somedir -print` > archive.sh
If you really want something even simpler, here's a dirt-simple version of shar as a shell script. It takes filenames on standard input instead of arguments for simplicity and to be a little more robust.
#!/bin/sh
while read filename
do
if test -d $filename
then
echo "mkdir -p '$filename'"
else
echo "sed 's/^X//' <<EOF > '$filename'"
sed 's/^/X/' < "$filename"
echo 'EOF'
fi
done
Invoke as:
find somedir -print | simpleshar > archive.sh
You still need to invoke sed, as you need some way of ensuring that no lines in the here document begin with the delimiter, which would close the document and cause later lines to be interpreted as part of the script. I can't think of any really good way to solve the quoting problem using only shell builtins, so you will have to rely on sed (which is standard on any Unix-like system, and has been practically forever).
if your problem are non-text-file-hating filters:
in times long forgotten, we used uuencode to get past 8-bit eating relays -
is that a way to get past attachment eating mail boxes these days ?
So why not zip and uuencode ?
(or base64, which is its younger cousin)

How to configure firefox to run emacsclientw on certain links?

I've got a Perl script that groks a bunch of log files looking for "interesting" lines, for some definition of interesting. It generates an HTML file which consists of a table whose columns are a timestamp, a filename/linenum reference and the "interesting" bit. What I'd love to do is have the filename/linenum be an actual link that will bring up that file with the cursor positioned on that line number, in emacs.
emacsclientw will allow such a thing (e.g. emacsclientw +60 foo.log) but I don't know what kind of URL/URI to construct that will let FireFox call out to emacsclientw. The original HTML file will be local, so there's no problem there.
Should I define my own MIME type and hook in that way?
Firefox version is 3.5 and I'm running Windows, in case any of that matters. Thanks!
Go to about:config page in firefox. Add a new string :
network.protocol-handler.app.emacs
value: path to a script that parse the url without protocol (what's after emacs://) and then call emacsclient with the proper argument.
You can't just put the path of emacsclient because everything after the protocol is passed as one arg to the executable so your +60 foo.log would be a new file named that way.
But you could easily imagine someting like emacs:///path/to/your/file/LINENUM and have a little script that remove the final / and number and call emacsclient with the number and the file :-)
EDIT: I could do that in bash if you want but i don't know how to do that with the windows "shell" or whatever it is called.
EDIT2: I'm wrong on something, the protocol is passed in the arg string to !
Here is a little bash script that i just made for me, BTW thanks for the idea :-D
#!/bin/bash
ARG=${1##emacs://}
LINE=${ARG##*/}
FILE=${ARG%/*}
if wmctrl -l | grep emacs#romuald &>/dev/null; then # if there's already an emacs frame
ARG="" # then just open the file in the existing emacs frame
else
ARG="-c" # else create a new frame
fi
emacsclient $ARG -n +$LINE "$FILE"
exit $?
and my network.protocol-handler.app.emacs in my iceweasel (firefox) is /home/p4bl0/bin/ffemacsclient. It works just fine !
And yes, my laptop's name is romuald ^^.
Thanks for the pointer, p4bl0. Unfortunately, that only works on a real OS; Windows uses a completely different method. See http://kb.mozillazine.org/Register_protocol for more info.
But, you certainly provided me the start I needed, so thank you very, very much!
Here's the solution for Windows:
First you need to set up the registry correctly to handle this new URL type. For that, save the following to a file, edit it to suit your environment, save it and double click on it:
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\emacs]
#="URL:Emacs Protocol"
"URL Protocol"=""
[HKEY_CLASSES_ROOT\emacs\shell]
[HKEY_CLASSES_ROOT\emacs\shell\open]
[HKEY_CLASSES_ROOT\emacs\shell\open\command]
#="\"c:\\product\\emacs\\bin\\emacsclientw.exe\" --no-wait -e \"(emacs-uri-handler \\\"%1\\\")\""
This is not as robust as p4bl0's shell script, because it does not make sure that Emacs is running first. Then add the following to your .emacs file:
(defun emacs-uri-handler (uri)
"Handles emacs URIs in the form: emacs:///path/to/file/LINENUM"
(save-match-data
(if (string-match "emacs://\\(.*\\)/\\([0-9]+\\)$" uri)
(let ((filename (match-string 1 uri))
(linenum (match-string 2 uri)))
(with-current-buffer (find-file filename)
(goto-line (string-to-number linenum))))
(beep)
(message "Unable to parse the URI <%s>" uri))))
The above code will not check to make sure the file exists, and the error handling is rudimentary at best. But it works!
Then create an HTML file that has lines like the following:
file: c:/temp/my.log, line: 60
and then click on the link.
Post Script:
I recently switched to Linux (Ubuntu 9.10) and here's what I did for that OS:
$ gconftool -s /desktop/gnome/url-handlers/emacs/command '/usr/bin/emacsclient --no-wait -e "(emacs-uri-handler \"%s\")"' --type String
$ gconftool -s /desktop/gnome/url-handlers/emacs/enabled --type Boolean true
Using the same emacs-uri-handler from above.
Might be a great reason to write your first FF plugin ;)