How do I pass a variable from AppleScript to a shell command? - shell

So far I have:
do shell script {var}
"if [[ ! -e /$var/ ]]; then
mkdir -p $var
fi"
I am getting an error of "Command not found"
I know I need to escape something, I'm just not sure where.
Var needs to be passed to the shell command, and create the directory in the specified folder if it doesn't already exist.

All you need is, e.g.:
set var to "/path/to/directory/subdirectory"
do shell script "mkdir -p " & var's quoted form
You do not need to test whether of not it already exists because of the use of the -p option with the mkdir command.
From the manual page for mkdir:
-p Create intermediate directories as required. If this option is not specified, the full path
prefix of each operand must already exist. On the other hand, with this option specified, no
error will be reported if a directory given as an operand already exists. Intermediate
directories are created with permission bits of rwxrwxrwx (0777) as modified by the current
umask, plus write and search permission for the owner.

Related

Bash Shell Script Issues

I am new to UNIX and have a homework assignment that is giving me trouble. I am to write a script that will back up specified files from the current directory into a specified destination directory. This script is to take three arguments.
sourcePath, which is the path to the source files/files being backed up or copied.
backupPath, which is the path to the target directory where the files will be backed up.
filePrefix, which is used to identify which files to backup, specifically only files whose names begin with the given prefix will be copied while others will be ignored. Example would be, if the user enters the letter "d", then all files starting with that letter are to be copied while any other file is to be ignored.
I haven't learned much about scripting/functions in bash so I've tried looking up tutorials which have been helpful but not enough. This script is something I can easily do when just typing out the commands. For instance, I would cd into the target directory that has the files, then using the cp command copy files that begin with the specific prefix to the target directory, but when making a script I am at a dead end.
I feel as though my code is monumentally incorrect and its due to my lack of experience, but nothing online has been of any help. So far my code is
read sourcePath
read backupPath
read filePrefix
grep /export/home/public/"$sourcePath
mkdir -p $backupPath
cp /export/home/public/"$sourcePath"/$filePrefix /home/public/"$backupPath"
So an example execution of the script would be
$ ./script.sh
(sourcePath)HW4testdir (backupPath)backup (filePrefix)d
Output:
backing up: def (example file starting with d)
backing up: dog (example file starting with d)
So far when executing the code, nothing happens. Again, I'm sure most, or even all of the code is wrong and totally off base, but I never learned about scripting. If I did not have to create a script, I could easily achieve this desired outcome.
I suggest with bash:
read -r -p "sourcePath: " sourcePath
read -r -p "backupPath: " backupPath
read -r -p "filePrefix: " filePrefix
mkdir -p /home/public/"$backupPath"
cp /export/home/public/"$sourcePath/$filePrefix"* /home/public/"$backupPath"
Make sure that the used user has the right to create the directory /home/public/"$backupPath".
See: help read
For a start: Your assignment states, that your script should accept arguments.
However your script does not take arguments. It reads the parameters from standard input. Arguments are passed to the script on the command line, and your script would be called as
./script.sh HW4testdir backup d
Hence you can't use read to fetch them. The first argument is available under the name $1, the second argument is $2 and so on. You could write for instance
sourcePath=${1?Parameter missing}
which has the side effect to abort the script with an error message, if the caller forgets to pass the parameter.
Another point: You don't say anywhere that bash should be used to run the script. Since you want the script to be called by
./script.sh ....
and not by
bash ./script.sh ....
you must encode the information, that bash should be used, in your script. Assuming that your bash is located in /usr/bin, you would do this by making the first line of the script
#!/usr/bin/bash

How to create and navigate instantly to a directory with Shell Scripting? [duplicate]

This question already has answers here:
Why can't I change directories using "cd" in a script?
(33 answers)
Change the current directory from a Bash script
(17 answers)
Closed 3 years ago.
I need to create and navigate to a directory with one line of command. And it needs to be inside a function. I'm new to terminal and shell scripting and don't know much about it.
My process goes like this:
I write the name of the directory and it is stored to $dirname variable, and then type "create" and it is stored to another variable named $thecommand, to create the directory with the name I gave, and navigate to it.
I have a create() function. Inside the function I did mkdir ./$dirname, and then cd $dirname. Then I applied a condition: if what the user wrote in $thecommand variable (BTW both $dirname and $thecommand are read, so the user writes the value) is equal to "create".
The directory was created, but it didn't navigate to it. So I tried creating an alias p="$dirname", and applied the alias in the function, still, it created the directory, but didn't navigate!
I would appreciate if someone helped me with this.
Here is my code:
echo "Write Folder name"
read dirname
echo "Write -create- to create the folder"
read thecommand
p="$dirname"
function create() {
mkdir ./"$p"
cd "$p"
}
if [ "$thecommand" == "create" ]
then
create
fi
There are a few things you can do.
First, just modifying the mkdir function in your .bashrc or .bash_aliases
function mkdir() {
command mkdir -p $1 # This creates the folder
command cd $1 # Then it immediately switches
command pwd # Remove this if you don't need confirmation. This just prints out current directory
}
If you don't want that to happen every time, you can just change the function name to create or whatever you prefer.
function create() {
command mkdir -p $1 # Note: the `-p` flag just creates a folder,
# like if you do `mkdir test/more`
# you will get an error that it is not valid.
# This does not change a regular `mkdir` command
command cd $1
command pwd
}
Or, you could just cd into the newly created folder

mkdir command not found in bash script

I don't know why I get error while running this simple script:
#!/bin/bash
read -p "Please enter directory name: " DIR
read -p "Please enter the path: " PATH
mkdir -p "$PATH/$DIR"
line 7: mkdir: command not found
Don't use the variable PATH. This variable contains a list of directories to search for executable programs. Since you're replacing it, the script can no longer find the mkdir program.
In general, avoid using variables that are all uppercase, these are often used as parameters for the shell or other programs.
The variable PATH is an important environment variable - it is the way that programs (like mkdir) are found, and you are overwriting it. You shouldn't do that, but if you must then:
/bin/mkdir -p "$PATH/$DIR"
but honestly DON'T USE UPPERCASE! There are loads of reserved or special variables in Bash, and if you can't remember them all then just remember that all except one is in UPPERCASE. Variables in Bash are case-sensitive, like in all sensible programming languages.

bash script doesn't find mkdir [duplicate]

This question already has answers here:
Getting "command not found" error in bash script
(6 answers)
Closed 2 years ago.
I've created a simple script to check if a folder exists and if not to create it. The script that follow
#!/bin/bash
PATH=~/Dropbox/Web_Development/
FOLDER=Test
if [ ! -d $PATH$FOLDER ]
then
echo $PATH$FOLDER 'not exists'
/bin/mkdir $PATH$FOLDER
echo $PATH$FOLDER 'has been created'
fi
works only if the mkdir command is preceded by /bin/. Failing in that, bash env output the error message "command cannot be found".
I though this could have been related to the system $PATH variable, but it looks regular (to me) and the output is as following:
/Library/Frameworks/Python.framework/Versions/2.7/bin:/bin:/usr/local/bin:/usr/bin:/sbin:/usr/local/sbin:/usr/sbin
I'm not sure whether the order with the different bin folders have been listed make any difference, but the /bin one (where the mkdir on my OSX Maverick) seems to reside is there hence I would expect bash to being able to execute this.
In fact, if I call the bash command from terminal, by typing just mkdir bash output the help string to suggest me how the mkdir command should be used. This suggests me that at a first instance bash is able to recognise the $PATH variable.
So what could be the cause? Is there any relation between the opening statement at the top of my .sh - #!/bin/bash - file and the "default" folder?
Thanks
Yeah, sometimes it is a bad idea to use capital letters for constant variables, because there are some default ones using the same convention. You can see some of the default variables here (Scroll to Special Parameters and Variables section). So it is better to use long names if you don't want to get any clashes.
Another thing to note is that you're trying to replicate mkdir -p functionality, which creates a folder if it does not exist (also it does create all of the parents, which is what you need in most cases)
One more thing - you always have to quote variables, otherwise they get expanded. This may lead to some serious problems. Imagine that
fileToRemove='*'
rm $fileToRemove
This code will remove all files in the current folder, not a file named * as you might expect.
One more thing, you should separate path from a folder with /. Like this "$MY_PATH/$MY_FOLDER". That should be done in case you forget to include / character in your path variable. It does not hurt to have two slashes, that means that /home/////////user/// folder is exactly the same /home/user/ folder.
Sometimes it is tricky to get ~ working, so using $HOME is a bit safer and more readable anyway.
So here is your modified script:
#!/bin/bash
MY_PATH="$HOME/Dropbox/Web_Development/"
MY_FOLDER='Test'
mkdir -p "$MY_PATH/$MY_FOLDER"
The problem is that your script sets PATH to a single directory, and that single directory does not contain a program called mkdir.
Do not use PATH as the name of a variable (use it to list the directories to be searched for commands).
Do learn the list of standard environment variable names and those specific to the shell you use (e.g. bash shell variables). Or use a simple heuristic: reserved names are in upper-case, so use lower-case names for variables local to a script. (Most environment variables are in upper-case — standard or not standard.)
And you can simply ensure that the directory exists by using:
mkdir -p ~/Dropbox/Web_Development
If it already exists, no harm is done. If it does not exist, it is created, and any other directories needed on the path to the directory (eg ~/Dropbox) is also created if that is missing.

Rsync copies too many directories being executed via bash script

Originally I would like to sync directory (with all files and subdirectories) given in parameter in bash script.
I found this post: How can I recursively copy a directory into another and replace only the files that have not changed? which explains how to use rsync in similar case.
My bash script is quite simple and listed below:
#!/bin/bash
echo -e "Type the project to be deployed: \c "
read project
echo -e "* Deploying: $project *"
echo -e "Sync: /var/repo/released/$project"
echo -e " /var/www/released/$project"
rsync -pr /var/repo/released/$project /var/www/released/$project
As a result it copies everything within /released (there are many directories in there, let's say -projects-).
I would like to copy (sync) only project given in parameter.
Could you please advice how to do this?
When you call the script without an argument (which most likely is what you're doing since you interactively read the project name into the variable $project), the positional parameter $1 remains empty. Therefore the script will rsync the entire content of /var/repo/released/.
You need to replace $1 with $project in your script. Also, I'd recommend to put double quotes around the paths to avoid problems due to spaces in a directory name.
rsync -pr "/var/repo/released/$project" "/var/www/released/$project"

Resources