1

I have recently started using Linux and I want to create a script that will do the following:

  1. Generate a text file where the first line is: Work Plan {todays date}
  2. Save it with the name work_plan_{todays date}
  3. (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?

Jacob Vlijm
  • 83,767
csss
  • 113
  • Well I did make an effort and I have created scripts to launch things like the settings panel. But I don't know how to do something as elaborate as generating a text file with a certain name and text already inside it. Also being able to run it from any terminal without its full path is something I want to also use for my existing scripts like the one for the settings panel. – csss Jul 12 '16 at 07:46
  • This might have already been answered here: http://askubuntu.com/questions/319910/creating-a-file-with-some-content-in-shell-scripting I hope it helps. Maybe you should ask something more specific. – Teddy Markov Jul 12 '16 at 07:46

3 Answers3

3

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
3

To do exactly what you describe takes a script

enter image description here

enter image description here

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-12
    
  • open 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)
            break

How to use

  1. The script needs both xdotool and wmctrl:

    sudo apt-get install xdotool wmctrl
    
  2. Copy the script into an empty file, save it as workplan (no extension!) in ~/bin. Create ~/bin if it does not exist yet. Make the script executable.
  3. 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"
    # ---
    
  4. 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 -l
    

    in 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
    
Jacob Vlijm
  • 83,767
1

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_$dat

To 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_command

after that make sure the file is executable, if necessary, run the following command

chmod +x /full/path/to/your/file

credits to c0rp for this answer https://askubuntu.com/users/164083/c0rp

Source, see the link below

How to run scripts without typing the full path?

jiipeezz
  • 1,256