Not unzipping all files while using 7z in shell script - linux

I am using below command
echo "A"|7z x -r -y /usr/sap/Silentinstall/commerce/commerce.7z.001
When I ran it from linux machine i.e. from cmd it works and it unzips all the files and folders.
But It it not extracting full files and folder when I am running it in shell script as below
#!/bin/sh
cd /usr/sap/Silentinstall/commerce
echo "A"|7z x -r -y /usr/sap/Silentinstall/commerce/commerce.7z.001
chmod -R 777 /usr/sap/Silentinstall/*

After lot of approaches i found below Solution,
cd /usr/sap/Silentinstall/commercedownloads
7z x /usr/sap/Silentinstall/commercedownloads/testcomm.7z.001 -o/usr/sap/Silentinstall/commercedownloads -aoa -r >>/usr/sap/Silentinstall/commercedownloads/log.txt
chmod -R 777 /usr/sap/*

Related

How to make shell script executable after decompress without chmod

I am studying linux, and I have to make all shell scripts executable by this command:
find ./ -name "*.sh" -exec chmod u+x {} \;
But when I download Logstash.tar.gz and extract it to /opt, all shell scripts were executable, no chmod needed. And /opt is not in $PATH.
echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin:/usr/lib/jvm/java-8-oracle/bin:/usr/lib/jvm/java-8-oracle/db/bin:/usr/lib/jvm/java-8-oracle/jre/bin
How do I build an app like logstash works?
===updated test steps for verification===
mkdir testtar && cd testtar
## create a executable script
echo test_with_x >> test_with_x.sh
chmod u+x test_with_x.sh
## create a not executable script
echo test_without_x >> test_without_x.sh
cd ..
## compress with gzip
tar -zcvf testtar.tar.gz ./testtar
mkdir testextract
mv testtar.tar.gz ./testextract
cd ./testextract
tar -zxvf testtar.tar.gz
## decompressed and see a executable and not executable script
Logstash tar bundle might have been created with executable permissions on all scripts. It should be the reason why it works fine after extracting without using chmod u+x command. If you also create a tar bundle with executable scripts, you should also get executable scripts after decompressing without using chmod u+x.
If you want your shell scripts to be executed without using chmod u+x, provide your shell script as an argument to your shell interpreter.
For e.g,
bash MyScript.sh
sh MyScript.sh
ksh MyScript.sh
You have two solution:
Add /opt to the PATH:
export PATH=${PATH}:/opt
call logstash with full path:
/opt/<anywhere.it.may.be>/logstash

linux command to copy contents of folder to root

I am using ubuntu I want to copy folder contents into another folder . I used below command
cp -R /home/user/public_html/domain.com /home/user/public_html/
But I get source and destinations are same error.
I would use tar (because it block copies). Something like
cd /home/user/public_html/domain.com
tar cf - * | (cd .. ; tar xvf -)
or (using your cp) like
cd /home/user/public_html/domain.com
cp -R * ../

bash script that works both on Ubuntu and MacOsX

I've created an install.sh files for my ubuntu machine. This file aims to start configuration of my dotfiles in an ubuntu machine. But I've also a MacOsX machine.
#!/bin/bash
clear
SCRIPT=$(readlink -f $0)
SCRIPTPATH=`dirname $SCRIPT`
cd $SCRIPTPATH && git submodule update -i
rm -rf ~/.vim
ln -s $SCRIPTPATH/.vim ~/.vim
rm ~/.vimrc
ln -s $SCRIPTPATH/.vimrc ~/.vimrc
rm ~/.bashrc
ln -s $SCRIPTPATH/.bashrc ~/.bashrc
This is what I get if I run same script in MacOsX:
readlink: illegal option -- f
usage: readlink [-n] [file ...]
usage: dirname path
fatal: Not a git repository (or any of the parent directories): .git
How can I convert my "install.sh", thus it can run both on MacOsX and Ubuntu?
Just use the first argument for the scriptname:
SCRIPT="$0"
$0 is always available and always contains the full script name. You only need readlink if you are executing the script from a link in order to return the actual target name of the script.

Copying files with wildcard (*) to a folder in a bash script - why isn't it working?

I am writing a bash script that creates a folder, and copies files to that folder. It works from the command line, but not from my script. What is wrong here?
#! /bin/sh
DIR_NAME=files
ROOT=..
FOOD_DIR=food
FRUITS_DIR=fruits
rm -rf $DIR_NAME
mkdir $DIR_NAME
chmod 755 $DIR_NAME
cp $ROOT/$FOOD_DIR/"*" $DIR_NAME/
I get:
cp: cannot stat `../food/fruits/*': No such file or directory
You got that exactly backwards -- everything except the * character should be double-quoted:
#!/bin/sh
dir_name=files
root=..
food_dir=food
fruits_dir=fruits
rm -rf "$dir_name"
mkdir "$dir_name"
chmod 755 "$dir_name"
cp "$root/$food_dir/"* "$dir_name/"
Also, as a matter of best-practice / convention, non-environment variable names should be lower case to avoid name conflicts with environment variables and builtins.

How to write a shell (.sh) script for the following

I'm new to linux/unix shell scripting, and I have a few dozen projects that I want to set up Subversion folders for (eventually I'll get to Git lol). How do I write a script to do the following:
Get a list of all sub-folders in a folder
For each sub-folder, use it execute the following commands:
svnadmin create /var/www/svn/<sub-folder>
svn import /var/www/<sub-folder> file:///var/www/svn/<sub-folder>
chmod -R 777 var/www/svn/<sub-folder>
chown -R apache.apache var/www/svn/<sub-folder>
From what I've seen on the internet so far, I suppose I put it all into a .sh file and type something like :
.sh thing.sh
... to execute it.
Any help appreciated.
for i in `find -maxdepth 1 -type d`; do
svnadmin create "/var/www/svn/$i"
svn import "/var/www/$i" "file:///var/www/svn/$i"
chmod -R 777 "var/www/svn/$i"
chown -R apache.apache "var/www/svn/$i"
done
Of course your svn import command is incorrect, and pathes in your chmod and chown missing /. But it's copypaste of your commands, anyway.
Does this work for you?
#!/bin/bash
for FILE in `ls`
do
if test -d $FILE
then
svnadmin create /var/www/svn/$FILE
svn import /var/www/$FILE file:///var/www/svn/$FILE
chmod -R 777 /var/www/svn/$FILE
chown -R apache.apache /var/www/svn/$FILE
fi
done
After saving execute chmod +x {name of file} on the script to make it executable with ./{name of file} or sh {name of file}.
In case you need all subfolders recursively from current folder:
#!/bin/bash
for FILE in `find . -type d`
do
if test -d $FILE
then
svnadmin create /var/www/svn/$FILE
svn import /var/www/$FILE file:///var/www/svn/$FILE
chmod -R 777 /var/www/svn/$FILE
chown -R apache.apache /var/www/svn/$FILE
fi
done
If you have any questions please comment.
You could create a script doIt.sh with the following:
#!/bin/bash
svnadmin create /var/www/svn/$1
svn import /var/www/$1 file:///var/www/svn/$1
chmod -R 777 var/www/svn/$1
chown -R apache.apache var/www/svn/$1
Then you can go into the folder in which you want to find all subfolders and execute the following:
find . -type d | xargs -I {} ./doIt.sh {}
Also, are you sure of this line:
svn import /var/www/<sub-folder> file:///var/www/svn/<sub-folder>
Did you not mean:
svn import /var/www/svn/<sub-folder> file:///var/www/svn/<sub-folder>
Note: Missing svn subfolder in path

Resources