I would like to open a file using the osx open command, but all I have is a variable containing the filename (and path) instead of the filename itself.
I tried:
thisfile=./filename.extension
open $thisfile
and
thisfile=./filename.extension
printf $thisfile | open
and
printf ./filename.extension | open
but in all of these attempts I just get
Usage: open [-e] [-t] [-f] [-W] [-R] [-n] [-g] [-h] [-b <bundle identifier>] [-a <application>] [filenames] [--args arguments]
Help: Open opens files from a shell.
... (full text: http://pastie.org/10074666)
What am I doing wrong? How can I open the file through a pipe and with a variable?
EDIT/solution:
I did have spaces (and parentheses) which I escaped with \ before storing in the variable. It turns out I should not have done that and I should have used open "${thisfile}" not open $thisfile
so for file
./foo - moo/zoo - boo (100)/poo (too).jpg
open with open via variable like this
thisfile='./foo - moo/zoo - boo (100)/poo (too).jpg'
open "${thisfile}"
As #mark-setchell commented about it, open does not take anything on stdin. So let's understand what would be wrong with the first scenario.
when you try to open a file using the open command on OSX, I see three main scenarios:
① the file does not exist or has spaces:
% open doesnotexists
The file /path/to/doesnotexists does not exist.
Usage: …
% open has spaces
The files /path/to/has and /path/to/pyodsdiff/spaces do not exist.
Usage: …
② the file contains a dash:
% open -notanoption
open: invalid option -- n
Usage: …
% open --notanoption
open: invalid option `--notanoption'
Usage: …
③ the variable contains nothing:
% open
Usage: …
So, it looks like it's ③! i.e.: however you're declaring your variable, you're failing to do it.
To test how you're declaring your variable, just use echo instead of open:
% thisfile=README.md echo $thisfile
% thisfile=README.md
% echo $thisfile
README.md
% thisotherfile=README.md ; echo $thisotherfile
README.md
If you have newline delimited filenames in a variable called filelist, then I think you would need to do something like this:
echo -e $filelist | while IFS=$'\n' read f; do open "$f"; done
Related
I am writing a .sh file to print the file names one by one. I have installed ubuntu in windows 10 and using the windows command prompt for executing below code. It is returning "E:/Official/Backups/GGG/*" instead of file names inside. I have also changed the EOL conversion to Unix(LF) by using notepad ++. please help.
#!/bin/bash
folder="E:/Official/Backups/GGG"
for entry in "$folder"/*
do
echo "$entry"
done
Running the script outputs:
$ bash test1.sh
E:/Official/Backups/GGG/*
Output of echo $-
himBHs
Output of ls -ld E:/Official/Backups/GGG
ls: cannot access 'E:/Official/Backups/GGG': No such file or directory
My bash in WSL does not recognize windows paths. If I want to access E:\Official\Backups\GGG I would have to use /mnt/e/Official/Backups/GGG.
I assume, the same goes for your WSL bash. Therefore the "path" E:/Official/Backups/GGG is just a non-existing directory and your observed behavior is to be expected. With bash's default settings a * just stays there as a literal if the directory does not exist or is empty. Example:
$ echo /dir/that/doesnt/exist/*
/dir/that/doesnt/exist/*
$ echo /dir/that/exists/but/is/empty/*
/dir/that/exists/but/is/empty/*
$ echo /dir/*
/dir/file1 /dir/file2 /dir/file3 ...
GGG folder is not exists. Please check and update with valid folder and try again.
#!/bin/bash
folder="E:"
for entry in "$folder"/*
do
echo "$entry"
done
I have a project that opens a file and writes some information to it. The file needs to exist, and needs to be empty. The file needs to be created the first time the program is run, and the file needs to be cleared for each successive run.
Is there an easy way in bash to create a file if it does not exist, else clear the contents of the existing file?
You don't need to use echo or any external utility, but just use the shell built-ins. Just use the shell re-direct operator (> file).
$ mkdir testDir
$ cd testDir/
$ ls -l
total 0
$ > fileThatDoesNotExist
$ ls -1
fileThatDoesNotExist
It will work even if there are some contents in the file, it will truncate its content
$ echo 'whatever' > fileThatDoesNotExist
$ cat fileThatDoesNotExist
whatever
$ > fileThatDoesNotExist
$ cat fileThatDoesNotExist
Creating a service in Automator (Run shell script > Bash) to copy to clipboard filesize (in bytes) of selected file in Finder. I'm not very well versed in bash, and can't see where this is wrong.
Aim is to have it appear in the 'services' menu when right clicking a file, and then I can just paste the filesize wherever I fancy.
Options I have selected in Automator are:
Service receives selected "files or folders" in "Finder.app"
Shell /bin/bash
Pass input as arguments
for f in "$#"
do
getty=$(ls -l "$f")
done
IFS=' ' read -a newList <<< "${getty}"
echo -n ${newList[4]} | pbcopy
Run Shell Script failed - 1 error:
-: -c: line1: syntax error near unexpected token `do
Instead of using ls, use stat to get the file size.
stat -f '%z' "$f" | pbcopy
To copy, say, the name and size of a group of files to the clipboard:
stat -f '%N %z' "$#" | pbcopy
To make it clearer to everyone, the solution is :
create an empty automator script
add action "get selected elements from Finder
add action "Run shell script" with inputs as arguments
with the following script :
stat -f '%z' "$#" | pbcopy
This question already has answers here:
How to get error output and store it in a variable or file
(3 answers)
Closed 8 years ago.
I am trying to store error meesage of a copy command in to a variable. But its not happening
Unix Command
log=`cp log.txt`
cp: missing destination file operand after `log.txt'
Try `cp --help' for more information.
echo $log
<nothing displayed>
I want to store above error message into a variable so that i can echo it whenever i want
Just redirect the stdout (normal output) to /dev/null and keep the stderror:
a=$(cp log.txt 2>&1 >/dev/null)
See an example:
$ a=$(cp log.txt 2>&1 >/dev/null)
$ echo "$a"
cp: missing destination file operand after ‘log.txt’
Try 'cp --help' for more information.
The importance to >/dev/null to keep away to normal output that in this case we do not want:
$ ls a b
ls: cannot access a: No such file or directory
b
$ a=$(ls a b 2>&1)
$ echo "$a"
ls: cannot access a: No such file or directory
b
$ a=$(ls a b 2>&1 >/dev/null)
$ echo "$a"
ls: cannot access a: No such file or directory
Note the need of quoting $a when calling it, so that the format is kept. Also, it is better to use $() rather than , as it is easier to nest and also is deprecated.
What does 2>&1 mean?
1 is stdout. 2 is stderr.
Here is one way to remember this construct (altough it is not entirely
accurate): at first, 2>1 may look like a good way to redirect stderr
to stdout. However, it will actually be interpreted as "redirect
stderr to a file named 1". & indicates that what follows is a file
descriptor and not a filename. So the construct becomes: 2>&1.
EDIT: I found a solution for this that worked. It's apparently important to quote the file paths to make sure they are read as a whole.
path_to_open=$(gfind ~/x/y/ | gshuf | ghead -n 1) && open "${path_to_open}"
I've written a command I want to use in osx's terminal. It gets a list of file and folder paths in a directory, shuffles them, and then gets the path that is in the first line of the txt after the shuffle.
This is what I've got so far: [1]
gfind ~/x/y/ | gshuf | ghead -n 1
With gfind ~/x/y/ | gshuf | ghead -n 1 > ~/Desktop/z.txt I get a file path in this format /Users/me/x/y/some folder/some file.txt Instead of writing the path of this file or folder to a .txt I want to open it as if I just double clicked on it in finder. How can I do that? I thought the open command[2] was the right one based on the description in the man page, but I'm not sure exactly how to use it. How should I use it? Or, if it's the wrong command, which command should I use?
[1]
I'm using gnu coreutils via macports which is why there's a g in front of the familiar command names
[2]
open [-e] [-t] [-f] [-W] [-R] [-n] [-g] [-h] [-b <bundle identifier>] [-a <application>] [filenames] [--args arguments]
Help: Open opens files from a shell.
By default, opens each file using the default application for that file.
If the file is in the form of a URL, the file will be opened as a URL.
Options:
-a Opens with the specified application.
-b Opens with the specified application bundle identifier.
-e Opens with TextEdit.
-t Opens with default text editor.
-f Reads input from standard input and opens with TextEdit.
-F --fresh Launches the app fresh, that is, without restoring windows. Saved persistent state is lost, excluding Untitled documents.
-R, --reveal Selects in the Finder instead of opening.
-W, --wait-apps Blocks until the used applications are closed (even if they were already running).
--args All remaining arguments are passed in argv to the application's main() function instead of opened.
-n, --new Open a new instance of the application even if one is already running.
-j, --hide Launches the app hidden.
-g, --background Does not bring the application to the foreground.
-h, --header Searches header file locations for headers matching the given filenames, and opens them.
To have the folder returned in the results opened in Finder, this should work:
open -a Finder `gfind ~/x/y/ | gshuf | ghead -n 1`
This works for me:
path_to_open=$(gfind ~/x/y/ | gshuf | ghead -n 1) && open "${path_to_open}"