How to pass a directory path containing spaces to the -C option of tar in a bash script? - bash

i'm building up in a bash script a tar command like this:
/usr/bin/tar -cvjf /tmp/archive.tar.bz2 -X excl.txt -C '/cygdrive/c/Users/Utente/dir with spaces/' dir1 dir2 dir2
/usr/bin/tar: '/cygdrive/c/Users/Utente/dir: Cannot open: No such file or directory
if i test the command in the shell it works fine.
if i run the script the script complains. i think that something fails when tar tries to change directory.
of course i can change directory in the script an then avoid the use of the -C option but i miss the opportunity to use the -X option.

Related

Creating multiple directories with shell script results in odd directory structure

I wrote a simple bash script to create some directories, like so:
#!/bin/sh
PROJECT_DIR=$(cd "$(dirname "$0")" && pwd)
cd ${PROJECT_DIR} || exit 1
mkdir -p Website/{static/{cs,js},templates/{html,xhtml}}
However, after I run the script (./script.sh), the directory structure looks like this:
There is no syntax error in my script. When I try the mkdir command directly on the terminal, the directories are created correctly.
Why is the bash script run behaving this way?

Extracting file with folders then using bash completion

I don't work much with bash scripting and was trying to do something like this:
#!/bin/bash
wget https://johnvansickle.com/ffmpeg/builds/ffmpeg-git-i686-static.tar.xz
tar xvf ffmpeg-git-i686-static.tar.xz
cd ./ffmpeg-git-20210501-i686-static/
cp ./ffmpeg-git-20210501-i686-static/ffmpeg /etc/bin
Is there a variable or a way I can determine what that extracted folder is called during script execution. For example with bash completion at the command line I would use cd ./ffmpeg (since I know it starts with ffmpeg)
Make sense?
There is no way to know beforehand the directory structure, but you can use a wildcard in the copy command:
cp ./*/ffmpeg /etc/bin
Although, I do not recommend installing tarball extracted executable within /etc/bin.
I'd put a symbolic link inside /usr/local/bin/ instead:
#!/usr/bin/env sh
__opwd="$PWD"
trap 'cd "$__opwd"' EXIT ABRT INT
wget https://johnvansickle.com/ffmpeg/builds/ffmpeg-git-i686-static.tar.xz || exit 1
# Create place where to install the unpacked archive
mkdir -p '/opt/ffmpeg-git-i686-static' || exit 1
# Unpack the archive
cd '/opt/ffmpeg-git-i686-static' || exit 1
tar xf "$__opwd/ffmpeg-git-i686-static.tar.xz" || exit 1
# Create a symbolic link from the ffmpeg command into `/usr/local/bin/`
ln -sf /opt/ffmpeg-git-i686-static/*/ffmpeg /usr/local/bin/

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

Including a chunk of code in a shell script

I have a number of shell scripts that all look like this:
#!/bin/bash
cd ~/Dropbox/cms_sites/examplesite/media
sass -C --style compressed --update css:css
cd ~/Dropbox/cms_sites/examplesite
rm -f ./cache/*.html
rm -fr ./media/.sass-cache/
rm -fr ./admin/media/.sass-cache/
rsync -auvzhL . username#host:/home/username/remote_folder
(I know the use of cd seems weird, but they have evolved!)
Now, all these scripts have a few differences, in that they have different usernames, hosts, local folder and remote folder names, and I want an inexperienced user to be able to run them without arguments (so he can drag and drop them into a terminal without issue).
What I'd like to do is something like:
#!/bin/bash
cd ~/Dropbox/cms_sites/examplesite/media
sass -C --style compressed --update css:css
cd ~/Dropbox/cms_sites/examplesite
include ~/scripts/common.sh
rsync -auvzhL . username#host:/home/username/remote_folder
then have a file in common.sh that looks like:
rm -f ./cache/*.html
rm -fr ./media/.sass-cache/
rm -fr ./admin/media/.sass-cache/
so that I can easily change sections of the code in lots of scripts at once.
Is this possible, or is there a better way to do this without using arguments and having one script?
Use the source command. It's bash's version of 'include'
No need for "include" if the script is executable:
~/scripts/common.sh
If the script is not executable or does not have an appropriate shebang line then you'll need to specify the interpreter:
bash ~/scripts/common.sh

Resources