How to make shell script executable after decompress without chmod - linux

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

Related

Not unzipping all files while using 7z in shell script

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/*

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

Need assistance for creating a simple bash script

I've created this bash file putting on it a secuence of commands i often run for synching files from my digital camera. the point is it doesn't to ANYTHING! What am i missing?
thank you!
code:
#!/bin/bash
#temporal
mkdir /tmp/canon
#copy files from camera
rsync -r /run/user/mango/gvfs/g*/DCIM /tmp/canon
cd /tmp/canon
#get files from subdirs
find ./ -name '*.JPG' -exec mv '{}' ./ \;
#remove dirs
ls -l | awk -F'[0-9][0-9]:[0-9][0-9]' '/^d/{print $NF}'| xargs -i rm -rf '{}' \;
#recreate folder structure with year|month pattern
jhead -n%Y/%m/%f *.JPG
#Sync with external HD
rsync -r --ignore-existing . /media/mango/WD/FOTOS/
If it does not even do the mkdir, then it sound most likely that the version of the script you want is not the one running. Try using an qualified path, such as ./myscript or an absolute path, like /home/joe/bin/myscript. The command type myscript will tell from where the shell is running it.
Also, try running the script after adding set -x to the top of the script or using bash -x myscript; that will show every line as it is executed.
If this still doesn't help, there could be bash startup code, such as in .bashrc getting in the way. That's much harder to diagnose, although the same set -x can be used, although with great caution unless a second user can login and edit this user's startup scripts since mistakes in startup scripts can make it impossible to login to the system.
Try this
chmod +x yourscriptname
./yourscriptname
Make usre you are running the same script you made.

Resources