I have recently started using Linux and I want to create a script that will do the following:
- Generate a text file where the first line is: Work Plan {todays date}
- Save it with the name work_plan_{todays date}
- (Optional - not essential like points 1 and 2) Open it full screen height, half screen width.
How can I write a script that will do this and that I can call from any terminal without having to out its full path? That is, I want to just type, for example, gen-work-plan and not /usr/home/document/gen-work-plan...
Does anyone know how I can do this?
23 Answers
To do exactly what you describe takes a script
Couldn't resist posting an answer...
To create a file, with the current date as a title and as header inside would take one or two lines. However, if you want to:
- prevent the file to be overwritten if you run the command accidentally
- open the file with gedit
- wait for the window to appear and subsequently move and resize it
...it will take a few lines more than that.
The script below will:
- create a new file, in an arbitrary directory, named after the current date, only if it doesn't exist yet. You wouldn't want to overwrite it.
add the current date to the file, like:
Workplan 12-12-12open the file with
gedit, wait for the window to appear (!), move the window to the upper left, resize it to 50% (width), 100% (height)
The script
#!/usr/bin/env python3
import time
import subprocess
import os
import time
# --- add the absolute path to the directory where you'd like to store the Workplans
filedir = "/home/jacob"
# ---
curr = "Workplan "+time.strftime("%d-%m-%Y"); out = curr+".txt"
file = os.path.join(filedir, out)
# check for the file to exist (you wouldn't overwrite it)
if not os.path.exists(file): # write "Workplan" + date to the file open(file, "wt").write(curr) # open the file with gedit subprocess.Popen(["gedit", file]) # wait for the window to appear, move/resize it t = 0 while t < 20: t += 1; time.sleep(1) wlist = subprocess.check_output(["wmctrl", "-l"]).decode("utf-8") if out in wlist: w = [l.split()[0] for l in wlist.splitlines() if out in l][0] for cmd in [["xdotool", "windowmove", w, "0", "0"], ["xdotool", "windowsize", w, "47%", "100%"]]: subprocess.call(cmd) breakHow to use
The script needs both
xdotoolandwmctrl:sudo apt-get install xdotool wmctrl- Copy the script into an empty file, save it as
workplan(no extension!) in~/bin. Create~/binif it does not exist yet. Make the script executable. In the head of the script, set the (absolute!) path to where you'd lioke to store your work plans:
# --- set the abs. path to where you'd like to store the "Workplans" filedir = "/home/jacob/Bureaublad" # ---Log out ands back in, run the command simply by:
workplan
Explanation
To create a file in python, we can simply use:
open("file", "wt").write("sometext")for the moving and resizing however, we need to wait for the window to appear by checking if the window appears in the output of the command:
wmctrl -lin the script:
while t < 20: t += 1; time.sleep(1) wlist = subprocess.check_output(["wmctrl", "-l"]).decode("utf-8") if out in wlist:Then once the window came into existence, we split off the window id (first string in the lines in the output of
wmctrl -l), move the window to the top left, and resize it:w = [l.split()[0] for l in wlist.splitlines() if curr in l][0] for cmd in [["xdotool", "windowmove", w, "0", "0"], ["xdotool", "windowsize", w, "47%", "100%"]]: subprocess.call(cmd) break
Something like:
#!/bin/bash
today=$(date "+%Y-%m-%d")
echo "Work Plan $today" > work_plan_"$today"
gedit work_plan_"$today"Don't forget to make it executable with chmod +x gen-work-plan.
To run it without path, either add the directory containing the scrip to your $PATH in ~/.bashrc (recommended for security):
export PATH=$PATH:_Location_of_the_file_Or symlink it in /usr/bin:
ln -s _location_/gen-work-plan /usr/bin/gen-work-plan Here is a simple bash script I would use
#!/bin/bash
dat=$(date "+%Y-%m-%d")
touch work_plan_$dat
echo "Work Plan $dat" > work_plan_$dat
if [ -f work_plan_$dat ]; then echo "Successfully created file work_plan_$dat"
fi
gedit work_plan_$datTo run this script without its path, its location should be included in $PATH environment variable. You can do it with this line
PATH=/an/example/path:"$PATH"You may want to add this line to your ~/.bashrc file
OR
You may create a symbolic link with this command
sudo ln -s /full/path/to/your/file /usr/local/bin/name_of_new_commandafter that make sure the file is executable, if necessary, run the following command
chmod +x /full/path/to/your/filecredits to c0rp for this answer
Source, see the link below
How to run scripts without typing the full path?
2