Bash Scripting: How to open Kate with directory and filename?

520 Views Asked by At

I am completely new to Bash scripting so bare with me while I try to describe this. What I want to do be able to insert two arguments. Arg1=Sub Folder in Documents Arg2=Title of the text file

Using Ubuntu 20.10. (If that matters) In short: kate ~/Documents/$Arg1/$Arg2 would be the equivalent to the command I would enter into my terminal.

The extra catch is having a keyword shortcut for the Sub Folder. For example say the Sub folder was name SUPERLONGNAME_EXTRALONG but I want a shortcut such as SHORTNAME=SUPERLONGNAME_EXTRALONG

#!/bin/sh

newfile()

    dirname=$1
    filename=$2

    if $dirname==dir1
        dirname=newdirname
        fi

kate ~/Documents/$dirname/$filename

This is basically what I have now. Although as you would guess this doesn't work. (Provided for aid in seeing what I am trying to do). I can open kate within the home directory with the file name of my choice. My real issue seems to be the creating a shortcut keyword. As well as having the file save to the Document/Arg1 directory. Please help.

I run the command through terminal using sh newfile arg1 arg2

1

There are 1 best solutions below

0
Allan Wind On BEST ANSWER

Use an associate array shortnames to optionally translate from long to short names. Verify that file and directory exist:

#!/bin/bash

declare -A shortnames=( 
  [long]="short"
)

d=$1
f=$2
if [ -z "$d" ]
then
    echo directory required
    exit 1
fi
if [ -n "${shortnames[$d]}" ]
then
    d=${shortnames[$d]}
fi
d=~/Documents/$d
if [ ! -d "$d" ]
then
     echo "directory $d does not exist"
     exit 1
fi

if [ -z "$f" ]
then
    echo file required
    exit 1
fi

kate "$d/$f"