create a shell script with a shell script (mac) - macos

I've been creating mac shell executables with this method:
Create a file;
Add #!/bin/sh;
Add the script;
Run chmod 755 nameofscript.
I now need to create a shell script to create a shell script in another directory and make it executable (with chmod) so that it can be run on startup.

#!/bin/sh
dir=/tmp
fnam=someshellscript
echo '#!/bin/sh' > $dir/$fnam
echo 'find /bin -name "*X*"' >> $dir/$fnam
chmod 755 $dir/$fnam

#!/bin/sh
echo "script goes here!" > /path/to/place
chmod 755 /path/to/place
?

Related

bash script through docker cant cd into existing directory

I have a docker file
which end with the following
COPY . /research
COPY ./funnel_viewer/run_dashboard.sh /
RUN chmod +x /run_dashboard.sh
ENTRYPOINT ["sh", "/run_dashboard.sh"]
the run_dashboard.sh script looks like:
#!/bin/bash
echo $(pwd)
echo $(ls -l)
cd ./funnel_viewer
/opt/conda/envs/dashboard_env/bin/python dashboard.py run
When running the docker I get 'cant cd to funnel_viewer'
This is while the echo ${ls -1} shows directory exists.
Directories owner is root and so is user running the script.
Why am I getting the cand cd to.. error?

How to make a bash script for mac?

I'm trying to make this bash script but get this: Error reading *.docx. The file doesn’t exist
Here's the script:
#!/bin/bash
textutil -convert txt *.docx
cat *.txt | wc -w
I'm currently running it from the folder but I'd like to make it a global script I can just call from any current folder.
If you want to make it available on your whole system you need to move it to a bin location like so
chmod a+rx yourscript.sh && sudo mv yourscript.sh /usr/local/bin/yourscript
then you can use it like a normal script in any folder

Bash Script to rename file inside a directory

I have a file named syscheck.sh in /system/0211/ and I want to rename it to checkone.sh.
How can I do it?
To do a rename in a bash script, you simply need to use the mv (move) command.
mv /system/0211/syscheck.sh /system/0211/checkone.sh
You can put this command inside a shell script myrenamescript.sh file like so:
#!/bin/bash
mv /system/0211/syscheck.sh /system/0211/checkone.sh
Now set the script as executable
chmod a+x myrenamescript.sh
Now you can run it:
./myrenamescript.sh

Convert bashrc function into a script/command?

I created the following function in a bash terminal as a way to move and immediately see the files in a directory.
function cnl { (cd $* ; pwd ; ls --color) }
It works fine as an addition to .bashrc, but I would like to turn it into a command that can be called from a script in my ~/bin directory.
Create a file in ~/bin with this content:
#!/bin/bash
cd $*
pwd
ls --color
and make it executable:
chmod u+x ~/bin/your_script

Why might "mkdir -p" lead to a Bash permission error?

If I make a directory with mkdir -p, it causes problems with scripts
$ mkdir -p test2/test2
$ cd test2/test2
$ echo '#!/bin/sh
> echo hello' > hello.sh
$ ./hello.sh
bash: ./hello.sh: Permission denied
This is nothing to do with mkdir. You simply haven't given hello.sh executable permissions. You need the following:
chmod +x hello.sh
Check your permissions
Check the permissions on your directories and the script itself. There may be a problem there, although it's unlikely.
ls -lad test2/test2
ls -l test2/test2/hello.sh
You can always use the --mode flag with mkdir if your permissions aren't being set correctly for some reason. See chmod(1) and mkdir(1) for more information.
Execute the file directly
You can execute the script with Bash directly, rather than relying on a shebang line or the executable bit, as long as the file is readable by the current user. For example:
bash test2/test2/hello.sh
Change file permissions
If you can execute the file when invoked explicitly with Bash, then you just need to make sure your file has the execute bit set. For example:
chmod 755 test2/test2/hello.sh

Resources