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

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

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"

Related

Make a program USB Copyer with bash shell linux

This is my code that I made to copy all fd data to the home/user directory with bash shell
#!/bin/sh
user=$(whoami)
now=$(date +"%d.%m.%Y_%H.%M.%S")
dir=$(mkdir $now) --> and here I want to create a folder as a place to copy the results according to datetime
if [ -d /media/$user/foo ]
then
echo "any media"
cp -r /media/$user /home/$user/$dir
else
echo "none of media attached"
fi
but i run thats code, cp -r /media/$user /home/$user/$dir not running as well, which is not copied to the newly created directory based on datetime , any advice ? or
something's wrong in my code ?
I suggest to replace
dir=$(mkdir $now)
with
mkdir "$now" || exit 1
dir="$now"
This will terminate your script if there is a problem with the directory creation.

script file not found when using source

I have a bash script in a file named reach.sh.
This file is given exe rights using chmod 755 /Users/vb/Documents/util/bash/reach.sh.
I then created an alias using alias reach='/Users/vb/Documents/util/bash/reach.sh'
So far this works great.
It happens that I need to run this script in my current process, so theoretically I would need to add . or source before my script path.
So I now have alias reach='source /Users/vb/Documents/util/bash/reach.sh'
At this point when I run my alias reach, the script is failing.
Error /Users/vb/Documents/util/bash/reach.sh:7: = not found
Line 7 if [ "$1" == "cr" ] || [ "$1" == "c" ]; then
Full script
#!/bin/bash
# env
REACH_ROOT="/Users/vb/Documents/bitbucket/fork/self"
# process
if [ "$1" == "cr" ] || [ "$1" == "c" ]; then
echo -e "Redirection to subfolder"
cd ${REACH_ROOT}/src/cr
pwd
else
echo -e "Redirection to root folder"
cd ${REACH_ROOT}
pwd
fi
Any idea what I could be missing ?
I'm running my script in zsh which is not a bash shell, so when I force it to run in my current process it runs in a zsh shell and does not recognize bash commands anymore.
In your question, you say "It happens that I need to run this script in my current process", so I'm wondering why you are using source at all. Just run the script. Observe:
bash-script.sh
#!/bin/bash
if [ "$1" == "aaa" ]; then
echo "AAA"
fi
zsh-script.sh
#!/bin/zsh
echo "try a ..."
./bash-script.sh a
echo "try aaa ..."
./bash-script.sh aaa
echo "try b ..."
./bash-script.sh b
output from ./zsh-script.sh
try a ...
try aaa ...
AAA
try b ...
If, in zsh-script.sh, I put source in front of each ./bash-script.sh, I do get the behavior you described in your question.
But, if you just need to "run this script in my current process", well, then ... just run it.
source tries to read a file as lines to be interpreted by the current shell, which is zsh as you have said. But simply running it, causes the first line (the #!/bin/bash "shebang" line) to start a new shell that interprets the lines itself. That will totally resolve the use of bash syntax from within a zsh context.

Bash does not detect that file exists

I have made a bash alias to execute scripts before and after the cd command. The only issue is it does not execute the files.
Here is my code
cd(){
if [ -d "./.dinit" ]
then
echo "dinit file exists"
source ./.dinit
fi
builtin cd $#
if [ -d "./.init" ]
then
echo "init file exists"
source ./.init
fi
}
In the .init file I have the command echo hello and in the .dinit I have echo goodbye
When I get this working I am going to put PATH= statements in the files along with alias.
Does anyone know how I can get the commands to execute?
Your checks right now look for directories, which you then try to source. That doesn't really make any sense, so I'm assuming that those are really the files you want to source. In which case, you should use -f to check for a regular file instead:
cd(){
if [ -f "./.dinit" ]
then
echo "dinit file exists"
source ./.dinit
fi
builtin cd "$#"
if [ -f "./.init" ]
then
echo "init file exists"
source ./.init
fi
}

Shell Script that monitors a folder for new files

I'm not a pro in shell scripting, thats why I ask here :).
Let's say I got a folder. I need a script that monitors that folder for new files (no prefix name of files is given). When a new file gets copied into that folder, another script should start. Has the second script processed the file successfully the file should be deleted.
I hope you can give me some ideas on how to achieve such script :)
Thank you very much in advance.
Thomas
Try this:
watcher.sh:
#!/bin/bash
if [ -z $1 ];
then
echo "You need to specify a dir as argument."
echo "Usage:"
echo "$0 <dir>"
exit 1
fi
while true;
do
for a in $(ls -1 $1/* 2>/dev/null);
do
otherscript $a && rm $a #calls otherscript with the file a as argument and removes it if otherscript returned something non-zero
done
sleep 2s
done
Don't forget to make it executable
chmod +x ./watcher.sh
call it with:
./watcher.sh <dirname>
try inotify(http://man7.org/linux/man-pages/man7/inotify.7.html)
or you may need to install inotify-tools (http://www.ibm.com/developerworks/linux/library/l-ubuntu-inotify/) to use it by shell.

Shell script file existence on Mac issue

Ok so I have written a .sh file in Linux Ubuntu and it works perfectly. However on a Mac it always returns that the file was not found even when it is in the same directory. Can anyone help me out?
.sh file:
if [ ! -f file-3*.jar ]; then
echo "[INFO] jar could not be found."
exit
fi
Just thought I'd add, this isn't for more than one file, it's for a file that is renamed to multiple endings.
In a comment to #Paul R's answer, you said "The shell script is also in the same directory as the jar file. So they can just double click it after assigning SH files to open with terminal by default." I suspect that's the problem -- when you run a shell script by double-clicking it, it runs with the working directory set to the user's home directory, not the directory where the script's located. You can work around this by having the script cd to the directory it's in:
cd "$(dirname "$BASH_SOURCE")"
EDIT: $BASH_SOURCE is, of course, a bash extension not available in other shells. If your script can't count on running in bash, use this instead:
case "$0" in
*/*)
cd "$(dirname "$0")" ;;
*)
me="$(which "$0")"
if [ -n "$me" ]; then
cd "$(dirname "$me")"
else
echo "Can't locate script directory" >&2
exit 1
fi ;;
esac
BTW, the construct [ ! -f file-3*.jar ] makes me nervous, since it'll fail bizarrely if there's ever more than one matching file. (I know, that's not supposed to happen; but things that aren't supposed to happen have any annoying tendency to happen anyway.) I'd use this instead:
matchfiles=(file-3*.jar)
if [ ! -f "${matchfiles[0]}" ]; then
...
Again, if you can't count on bash extensions, here's an alternative that should work in any POSIX shell:
if [ ! -f "$(echo file-3*.jar)" ]; then
Note that this will fail (i.e. act as though the file didn't exist) if there's more than one match.
I think the problem lies elsewhere, as the script works as expected on Mac OS X here:
$ if [ ! -f file-3*.jar ]; then echo "[INFO] jar could not be found."; fi
[INFO] jar could not be found.
$ touch file-302.jar
$ if [ ! -f file-3*.jar ]; then echo "[INFO] jar could not be found."; fi
$
Perhaps your script is being run under the wrong shell, or in the wrong working directory ?
It's not that it doesn't work for you, it doesn't work for your users? The default shell for OS X has changed over the years (see this post) - but it looks like your comment says you have the #! in place.
Are you sure that your users have the JAR file in the right place? Perhaps it's not the script being wrong as much as it's telling you the correct answer - the required file is missing from where the script is being run.
This isn't so much an answer, as a strategy: consider some serious logging. Echo messages such as "[INFO] jar could not be found." both to the screen and to a log file, then add extra logging, such as the values of $PWD, $SHELL and $0 to the log. Then, when your customers/co-workers try to run the script and fail, they can email the log to you.
I would probably use something like
screenlog() {
echo "$*"
echo "$*" >> $LOGFILE
}
log() {
echo "$*" >> $LOGFILE
}
Define $LOGFILE at the top of your script. Then pepper your script with statements like screenlog "[INFO] jar could not be found." or log "\$PWD: $PWD".

Resources