Bash script to mkdir - macos

I am trying to create a directory based on a variable entered by a user and then save files there.
I thought this would be simple enough for me but I get an error message "No such file or directory" when I try to save there.
When I hit "ls" it lists the directory with a "?" after it.
I am working with an .sh script on a Mac terminal.
Relevant code:
#get user input
echo "enter the collection number"
read COLLECTION
#create the directory
mkdir "$COLLECTION"dir
#calculate a checksum and save it to the above directory
sudo openssl md5 /dev/disk1 > "$COLLECTION"dir/md5.txt
--

Check you script to see if you have DOS style line endings (\r\n). You can safely run dos2unix on the script if you aren't sure.
The ? you see in the file name may actually be the carriage return at the end of the line (since Bash doesn't treat that as whitespace).
So "$COLLECTION"dir/ doesn't exist; "$COLLECTION"dir\r/ does.
Edit:
Vi usually does a good job showing you what those special characters are.
ls | vi -

The only piece of this code likely to give you a "No such file or directory" error is the last line. Does /dev/disk1 exist on your machine?

I use mkdir -p when I get that error ;)

Related

Cygwin on Windows 7 (64bit): No such file or directory - but 'which' does give me the correct path

A formely working bash script no longer works after switching computers. I get the following error:
No such file or directory.
Before going on, please excuse any mistakes you may find since english is not my native language.
The script was used in cygwin under Windows XP. I now had to switch to cygwin64 under Windwos 7 (64bit).
The script is used as a checkhandler for the program SMSTools3 to split a file with a specific format into multiple smaller ones, which the program then uses to send SMS to multiple recipients. The script was copied directly from the page of SMSTools3 and uses the package formail.
After looking up the error the most likely problem was that the environmantle path was not set up to look in the right path (/usr/bin). I therefore added it to the path but to no avail.
I then deleted other entries in the enviromental path of windows which contained spaces because that could have been another explanation, but again to no avail.
Following is a minimal example of the code which produces the error.
#!/bin/bash
# Sample script to allow multiple recipients in one message file.
# Define this script as a checkhandler.
echo $PATH
which formail
outgoing="/var/spool/sms/outgoing"
recipients=`formail -zx "To:" < "$1"`
I added the lines the lines echo $Path and which formail to show if the script can find the correct file. Both results look fine, the second command gives me the right output '/usr/bin/formail'
But the line recipients=... throws me the error:
No such file or directory.
I do not have much experience with bash scripting, or cygwin in general. So if someone on this wonderful board could help me solve this problem, I would be really grateful. Thank you all for your help.
EDIT:
First of all thank you all for your comments.
Secondly, I would like to apologize for the late reply. The computer in question is also used for other purposes and my problem is part of a background routine, so I have to wait for "free time" on the pc to test things.
For the things #shellter pruposed: The ls command returned an error: '': No such file or directory.
The which -a formail as well as the echo $(which -a formail) commands that #DougHenderson pruposed returned the 'right' path of /usr/bin/formail. echo \$1 = $1 before the recipent line returned the path to the checkhandler file (/usr/local/bin/smsd_checkhandler.sh), the same command after the recipent line seems to show a empty string ($1 = ). Also, the pruposed change to the recipent line did not change the error.
For the dos2unix conversion that #DennisWilliamson pruposed, I opened the file in notepad++ to use their build in converion, but it showed me that the file is in unix format with Unix style line endings.

Variable for a right-clicked item (say, a jpg) in a bash script?

I have a very simple bash script that I run often from the cli, but I've found it's frustrating to have to open a terminal, identify the right file, and run it and think the easiest way would be to run it as an option from a right-click. I am running Ubuntu 18.04 LTS.
The script is just erasing exif data, leaving the orientation tags, essentially this:
exiftool -all= -tagsfromfile # -Orientation file-*.jpg
Is there a way to have the script identify which image I'm right clicking on? I'm at a loss what to put in the file-*.jpg part which will be a variable for "whatever image I'm right-clicking on right now."
Tried searching for a good while on how to do this but am clearly either not using the right search terms or else this isn't done very often. Thank you in advance for any help!
if you want your script to run in file manager right-click menu you have to change your script and define file(s) as arguments. this happens simply by changing your file section with $1 to $n as the parameter(s).
as far as I know ubuntu uses nautilus as an file manager.
you can run nautilus-actions-config-tool either from your terminal or from dash and define your script a name and a command to run. you can follow this link for illustration learning :
ubuntu nautilus defile script in menu
for example :
#!/bin/bash
if [ "$1" != "" ]; then
echo "Positional parameter 1 contains value $1"
else
echo "Positional parameter 1 is empty"
fi
for all arguments :
#!/bin/bash
if [[ "$#" -gt 0 ]]; then
for arg in "$#"; do
echo $arg
done
fi
here is the image that shows the script worked
I know the question is a little older, but I can provide you with the solution.
You have to set up FileManager-actions, an extension for GNOME/Nautilus (but it also works for other file managers).
Setup filemanager-actions on Ubuntu 20.04
sudo apt update
sudo apt install filemanager-actions
Then run fma-config-tool and create a new action.
When creating an action, please ensure that:
[v] Display item in selection context menu
is flagged; otherwise, you will not see the context menu during the file selection.
Prepare the script you want to execute
Prepare a script that does what you need. Touch in /tmp, mv it in /usr/bin and give it execute permissions:
touch /tmp/your-script
# edit it with your editor
sudo mv /tmp/your-script /usr/bin/
sudo chmod +x /usr/bin/your-script
In your script, you can reference the filename using $1.
FILENAME=$1
echo $FILENAME
In the variable FILENAME you will find the selected file name.
Configure Nautilus-action command
To let nautilus pass the filename, insert the script path and the argument string in the' command' tab.
To fully answer your question, to let Nautilus pass the filename as a script argument, you have to specify %f.
At this point, quit the Nautilus instance and open it again:
nautilus -q
nautilus
Now, let's have a try! Right-click on a file and check out the result!
Appendix 1 - Filemanager-actions formats
%f A single file name, even if multiple files are selected.
%F A list of files. Each file is passed as a separate argument to the executable program.
%u A single URL. Local files may either be passed as file: URLs or as file path.
%U A list of URLs. Each URL is passed as a separate argument to the executable program.
%d Base directory
Here you can find a comprehensive list.
Appendix 2 - Sources
Check out my post blog in which I actually realize something similar: https://gabrieleserra.ml/blog/2021-08-14-filemanager-actions-ubuntu-20-04.html
Reference to all possible formats for FileManager-actions: https://askubuntu.com/a/783313/940068
Realize it in Ubuntu 18.04: https://askubuntu.com/a/77285/940068

bash "command not found" - is my PATH wrong [duplicate]

This question already has answers here:
Bash script prints "Command Not Found" on empty lines
(17 answers)
Closed 2 years ago.
I'm using bash for the first time. Wrote a code that is supposed to take "stats" as a command, but whenever I use "stats" in my command lines I kept getting the following error:
bash: stats: command not found
I googled around and a lot of people are saying this error is usually associated with PATH problems. Running "echo $PATH" yields the following results:
/bin:/sbin:/usr/local/bin:/usr/bin:/usr/local/apps/bin:/usr/bin/X11:/nfs/stak/students/z/myname/bin:.
I made sure my program started with
#!/bin/bash
Is my PATH wrong? If so, how do I fix it? If not, any suggestions on what else I should look into? Thank you all for your time and help.
It might be a PATH problem, it might be a permission problem. Some things to try.
1) If stats is not in your current directory, change directory (cd) to the directory where stats is and do
bash stats
If stats executes correctly, then you know at least that the script is OK. Otherwise, look at the script itself.
2) Try to execute the script with
./stats
If this gives
bash: ./stats: Permission denied
Then you have a permission problem. Do a
chmod a+rx stats
and retry. Note: a+rx is perhaps a bit wide; some may suggest
chmod 755 stats
is a better choice. Hint: from the comments, I see that this is one of your problems.
3) From the name of the directory, I get the impression that the file is on NFS. It might therefore be mounted as 'noexec', meaning that you cannot execute any files from that mount. You might try:
cp stats /tmp
chmod 700 /tmp/stats
/tmp/stats
4) Check the full path name for stats. If you are still in the same directory as stats, try
pwd
Check if this directory is present in the PATH. If not, add it.
export PATH=$PATH:/nfs/stak/students/z/myname/344
and try stats again.
Your PATH looks reasonable. PATH problems are a common source of errors like this, but not the only one.
The error message ": No such file or directory" suggests that the script file has DOS/Windows-style line endings (consisting of a carriage return followed by linefeed) instead of unix-style (just linefeed). Unix programs (including shells) tend to mistake the carriage return for part of the line, causing massive confusion.
In this instance, it sees the shebang line as "#!/bin/bash^M" (where "^M" indicates the carriage return), goes looking for an interpreter named "/bin/bash^M", can't find it, and prints something like "/bin/bash^M: No such file or directory". Since the carriage return makes the terminal return to the beginning of the line, ": No such file or directory" gets printed on top of the "/bin/bash" part, so it's all you see.
If you have the dos2unix program, you can use that to convert to unix-style line endings; if not, there are a variety of alternate conversion tools. But you should also figure out why the file has Windows/DOS format: did you edit it with a Windows editor, or something like that? Whatever caused it, you should make sure it doesn't happen again, because Windows/DOS format files will cause problems with most unix programs.

Copying .jpg file using shell script gives 'Failed to open input stream for file' error

Very simple script to copy a file
#!/bin/bash
#copy file
mtp-getfile "6" test2.jpg
I set it as executable and run it using
sudo sh ./test.sh
It gives me a file called test2.jpg that has no icon and I cannot open I get a 'Failed to open input stream for file' error
However, if I simply issue the following from the command line
mtp-getfile "6" test2.jpg
It works as expected. What is wrong with my script? I checked and the resulting .jpg file in each case has the same number of bytes. Very strange.
As commented by chepner, your file might have a DOS (Windows) invisible line ending on its name, which would cause an error. To get rid of this unwanted character(s), just create a new blank script on your "nix" system and type the name by hand (not by copying and pasting, to avoid problems), let's say, name it test2.sh.
Then copy all the contents of test.sh to test2.sh (copy and paste) and run test2.sh and see if it works. If it doesn't, try running the following code on the new script, to make sure that there are no unwanted characters on the code itself:
tr -d "\r" < /folder/test2.sh && echo >> /folder/test2.sh
And then try to run script2.sh again to see if it works. Note: the echo >> /folder/test2.sh part of the code above is just to make sure that your new script ends with a newline, which is a Posix standard (and without which some programs may misbehave because they expect the file to end with a newline).
Apparently it was a permissions issue.
I only had to do a sudu chown test2.jpg

Deleting files from a list using shell

I am a beginner with Applescript & Shell and am writing a script that at a certain point requires me to delete files that are listed within a .txt file. I have searched extensively on stackoverflow and was able to come up with the following command that I am running from within my Applescript...
do shell script "while read name; do
rm -r \"$name"\
done < ~Documents/Script\\ Test/filelist.txt"
It seems to recognize and read the file, but I get an error that says this and I cannot figure out why:
error "rm: ~/Documents/Script Test/filetodelete.rtf: No such file or directory" number 1
That said, I can navigate to that exact directory and verify that a file by that name with that extension does indeed exist. Can someone help shed some light on why this error might be occurring?
You have a typo. The path to the file is most probably ~/Documents, not ~Documents (which in Bash would be the home directory of a user whose account name is Documents).
If your shell is not Bash, it might not even support ~ for $HOME.
In the data file, you also cannot use ~ to refer to your home directory. You could augment the loop with a simple substitution to support this:
while read -r file; do
case $file in '~'*) file=$HOME${file#\~};; esac
rm -r "$file"
done < ~/"Documents/Script Test/filelist.txt"
Notice also the use of read -r to avoid some pesky problems with the legacy default behavior of read.

Resources