What I'm trying right now:
dir=$(pwd)
dir=${$dir//\//:}
But this isn't working for some reason.
The whole point of this is to convert a bash path to an applescript path, without hardcoding the path in there.
I'm essentially trying to do the opposite of this:
https://apple.stackexchange.com/questions/4938/how-to-change-applescript-path-to-a-terminal-style-path
Your syntax is just a bit off, don't use $ on the variable within the ${} construct:
dir=${dir//\//:}
Related
I've been trying to customize my bash command prompt with a shell script for some time now, but haven't had much success with it.
I'm trying to turn the prompt into something like this, with the Pos variables being strings or bash commands I've defined into the code.
PS1="[$PosZero][$PosOne][$PosTwo][$PosThree]$"
One thing I've tried to do save the current command prompt to a variable, and try and see if I can edit the contents of the brackets somehow, like so:
DEFAULT=$PS1
But what I've been struggling with is trying to edit the custom command prompt after having applied one already. If I'm attempting to change just one bracket, I want all the other brackets to keep whatever contents they have at the time. Instead, they erase themselves unless I pass the same information into the variables in the script.
I've been trying to find a way to parse the DEFAULT value (with the contents of PS1 within) to take out the contents of the brackets and apply them to the Pos variables. But I do not know how to do this. Does someone know how?
I think, this is what you want:
$ PS1='[$PosZero][$PosOne][$PosTwo][$PosThree]$ ' #Note: Use single quotes here, or escape $ as \$
[][][][]$ PosZero=abcd
[abcd][][][]$ PosOne=pqrs
[abcd][pqrs][][]$ PosZero=1234
[1234][pqrs][][]$ PosTwo=xyz
[1234][pqrs][xyz][]$ unset PosZero
[][pqrs][xyz][]$ PosOne=
[][][xyz][]$
I am writing my own unix scripts so I want to add a new directory for Bash. I add sth in .bash_profile like this.
PATH="~/Documents:${PATH}"
export PATH
and in my ~/Documents, there is a file named test of which the content is
#!/usr/bin/env python3.5
print("hahahhah")
I also used
chmod 755 test
to make it executable.
But I cannot call it in terminal directly. ./test works as usual.
What went wrong?
After I change to
PATH="$HOME/Documents:${PATH}"
export PATH
nothing happens.
FDSM_lhn#9-53:~/Documents$ test
FDSM_lhn#9-53:~/Documents$ ./test
hahahhah
Solution:
The fundamental reason is that I have a command of the same name as default one, So it won't work any way! Changing name will be sufficient!
Tilde doesn't get expanded inside strings. So by quoting the right-hand side of the assignment you prevent it from being expanded and get a literal ~ in your PATH variable which doesn't help you any.
You have two ways to fix this:
Drop the quotes on the assignment (yes this is safe, even for $PATH values with spaces, etc.).
Use $HOME instead of ~.
I prefer the second solution but the first is entirely valid for this case.
Beware though that in places where you aren't doing a straight assignment you often cannot just drop the quotes and trying to use ~ will cause problems.
In which case you will end up finding a question like this with an answer like this and something ugly like this.
I have a shell script that I'm trying to write to a file using multiple variables, but one of them is being ignored.
#!/bin/bash
dir=/folder
name=bob
date=`date +%Y`
command > $dir/$name_$date.ext
The $name is being ignored. How can I fix this?
Have you noticed that the _ was "ignored" as well? That's a big hint.
If you use set -u, you'll see the following:
-bash: name_: unbound variable
The way bash parses it, the underscore is part of the variable name.
There are several ways to fix the problem.
The cleanest is the ${var} construct which separate the variable name from its surroundings.
You can also use quotation in various ways to force the right parsing, e.g.: "$dir/$name""_$date.ext"
And in case your variables might contain spaces (now, or in the future) use quotation for words.
command >"$dir/${name}_$date.ext"
command >"${dir}/${name}_${date}.ext"
Both these are fine, just pick one style and stick to it.
I'm working on what should be a very simple BASH script. What I want to do is a pull an image from a webcamera using curl and write it to a file whose name is datestamped.
#! /bin/bash
DATE=$(date +%Y-%m-%d_%H-%M)
DIRECTORY1=home/manager/security_images/Studio_1/
TARGET1=${DIRECTORY1}${DATE}.jpg
curl http://web#192.168.180.211/snapshot.cgi > $TARGET1
When I try to run this I am told that there is no such file or directory. I believe this is due to an error in my escaping but I have tried seemingly every combination of quotation marks around the variables at each stage and still can't get it to work. I just don't understand what is going wrong and could really use some pointers towards what I'm doing wrong.
Many thanks
No, it’s just a typo.
DIRECTORY1=home/manager/security_images/Studio_1/
^^
Should be
DIRECTORY1=/home/manager/security_images/Studio_1/
of course.
As for escaping, even though only safe characters are used now, so quotes are technically superfluous, quoting out all $variables in every line by default is a good habit in shell scripting, there are very few cases when you do not want to use them.
Double quoting the redirection target should be enough:
curl http://web#192.168.180.211/snapshot.cgi > "$TARGET1"
Just make sure the path to it exists. You can run your script with
set -xv
to see how variables are interpolated.
I using script to apply folder permission. The script is working fine for applying permission.
On the script I just need to key in the complete path of directory.
However I meet challenge where the directory name is using $. e.g. G:\$Tobe-Review
Mean while on the script:
$PATH = "G:\$Tobe-Review"
The output of course give an error.
How to counter the $ sign for the folder name?
You could use the single-quote to prevent from interpolate the $. Difference between single and double quotes in bash might be very helpful to you to understand the reason.
Enclose your path in single-quotes.
The answer:
$PATH = 'G:\$Tobe-Review'