high sierra bash shell script how to change directory - bash

I work with high sierra and I have the following shell script
TOMCAT_PATH=/usr/local/Cellar/tomcat/9.0.12/
cd ./webapp/inventory/WEB-INF
rm -r -v classes
mkdir classes
cp -R ../../../classes ./classes
cd ..
jar -cvf inventory.war WEB-INF
cp inventory.war $TOMCAT_PATH/webapps
cd ../..
How can I execute the command
cd ./webapp/inventory/WEB-INF
After this cd the directory doesn't change
The current directory
/Users/carlotavina/Documents/carlota/irvine/javaenterprisedevelopment/curso6-webservices/week2/assignment/HW-2_Setup
In this directory I have subdirectory webapp/inventory. I want to move to this directory

Related

get folder name with find ... -name command in bash

Trying to get name of venv folder for activating it in bash script located inside the project. Because someone could use for venv another name like 'someones_env'
projectdir=$(cd ../../ && pwd)
echo "$(dirname "$projectdir")"
venvdir=$(find "$(dirname "$projectdir")" -name '*env')
echo "$(dirname "$venvdir")"
source "$(dirname "$venvdir")"/bin/activate
but $venvdir becomes the same as $projectdir instead of 'someones_env'
what am i doing wrong?
thanks
The directory of the *env folder is the project directory, that's why dirname "$venvdir" becomes the same as the project directory. Without dirname it should work.
Also, the directory of ../../ is ../../../, this might also be giving you some issues. If you are already in a directory, there's no need to use dirname. dirname calculates the parent directory.

How to make this command work from parent directory zip files/test.zip $(tar tf files/test.gz)

I've this command:
zip files/test.zip $(tar tf files/test.gz)
But it doesn't work because everything from $(tar tf files/test.gz) are in files/
So zip can't find it.
It works perfectly if I change directory to files and exec this one:
zip test.zip $(tar tf test.gz)
But I need to make it work from parent directory.
My full command is:
tar xzf files/test.tar.gz && zip files/test.zip $(tar tf files/test.tar.gz) && rm -r -- $(tar tf files/test.tar.gz)
From Is there any command on Linux to convert .tar.gz files to .zip?
Thank you
I would make a little script for that. If you have many tgz files to handle, you can apply a loop on it.
#!/bin/bash
TMP=/a/tmp/directory
TARGET=/dir/to/your/files
mkdir -p $TMP
cd $TMP
tar -C $TMP -xzf $TARGET/your.tar.gz && zip $TARGET/your.zip *
rm -rf $TMP

symlink in particular directory using rpm spec

rpm created using spec file will create directory "directory1" and all files in /var/lib/directory1.
For another use case i want to create another directory in "/var/lib" which should be a symlink to directory1.
eg:
cd /var/lib/
ls -la
directory2 -> directory1
directory1
how is it possible to do achieve this without using absolute paths in spec file?
%install
mkdir -p %{buildroot}/%{_sharedstatedir}/directory1
ln -s directory1 %{buildroot}/%{_sharedstatedir}/directory2
%files
%{_sharedstatedir}/directory1
%{_sharedstatedir}/directory2

Why is my link broken? Bash

I am trying to link files in a while loop for my script but just the simple linking code itself creates a broken link.
The directory structure is this:
main/working/script.sh
main/working/dir
main/shared/default/some_files
My script has this code:
ln -s ../shared/default/* dir
This creates broken link. I can make the link not broken if I go inside the directory of main/working/dir and use ln -s ../../shared/default/* .
That is because you link to a relative path; Inside your script go to main/working/:
cd main/working/
ln -s ../shared/default/* dir
either use the absolute path:
ln -s /absolute/path/to/shared/default/* dir
you might even deduce the path where your script is located and use that path:
DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
ln -s $DIR/../../shared/default/* dir
edit: bash cannot expand the * if you are not at the right directory, so you can work around that to temporarily change directories:
# go to dir to make correct relative links
cd dir
ln -s $DIR/../../shared/default/* ./
cd ..

copying a directory files into current directory

If I have a directory "test" and inside a directory "new" and I want to copy all files from "new" to test(my current directory).
I tried: cp -r new . because I know dot is like current directory but it didn't work.
Assuming you are in "test":
cp -r new/* .
To test:
mkdir test
cd test
mkdir new
touch new/1
touch new/2
touch new/3
cp -r new/* .
Output:
$ ls
1 2 3 new
This will do it:
cp -rf present/directory /desire/directory

Resources