Executing shell script in ubuntu - linux

Maybe I am missing something here but I have the following small bash script to delete some old files that get created when using flex and yacc its pretty simple but when I run the script it echos the result to the terminal but does not delete the file I'm probably missing something stupid could you guys point me in the right direction.
#!/bin/bash
echo "rm -f y.tab.h"; (just using one file for now)
I tried changing it to
echo rm -f y.tab.h;
but still no luck
I tried executing it with bash delete.sh and sh delete.sh and even using chmod +x on the file and executing it with ./

Remove echo statement
Just use
rm -f y.tab.h

Related

rm doesn't Work in script bash

I have a sh file with 1.000.000 of instructions "rm -f", but is not working. This runs on a ubuntu server 16.04
I tried this.
#!/bin/bash
/bin/rm -f /media/pictures2015/pictures/FTP/20150806/5939757.tif
/bin/rm -f /media/pictures2015/pictures/FTP/20150806/5939758.tif
/bin/rm -f /media/pictures2015/pictures/FTP/20150805/5939759.tif
And this
#!/bin/bash
rm -f /media/pictures2015/pictures/FTP/20150806/5939757.tif
rm -f /media/pictures2015/pictures/FTP/20150806/5939758.tif
rm -f /media/pictures2015/pictures/FTP/20150805/5939759.tif
The sh file has all the permissions, but don't wokr. With the "split" command change the sh file of 1.000.000 lines to two sh files of 500.000 lines.
The output dont show any error !!!
Thank You
the error was caused because the list of the images exported from phpmyadmin and at the end of the line I am with the "CR-LF" format, so investigate how to remove it and find the dos2unix program, install it in ubuntu, run and it worked.
The final solution I found on this link cyberciti
Thanks !!!!

Bash script change working directory to the directory it is in

I read Can a Bash script tell what directory it's stored in?, it shows I can get script directory by DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )".
I find I can use cd command to change working directory.
This is the content of import.sh. It is in /Users/gqqnbig/SourceCode/Java/PlayerStatisticReader/bin.
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
cd DIR
java -cp .;jsoup-1.7.3.jar;mysql-connector-java-5.1.29-bin.jar globalVisionEntertainment.nba.Program %1
This is what I get after executing the script.
Macintosh:PlayerStatisticReader gqqnbig$ pwd
/Users/gqqnbig/SourceCode/Java/PlayerStatisticReader
Macintosh:PlayerStatisticReader gqqnbig$ bin/import.sh
: command not found 2:
: No such file or directoryDIR
: command not found 4:
I execute it in the default terminal in Macintosh.
Why is the command not found? How can I make it work?
you need to write
cd "$DIR"
strictly, you only need to add the dollar, but you should also quote the path because it may contain spaces. as to the command not found messages; I don't know. You can remove the empty lines. my guess is it's an encoding issue. do you get the "command not found" output if you paste the script directly into your terminal instead of running the file?
use the following to expand the variable
cd ${DIR}
For what it's worth, I was looking for a solution on how to schedule a command with specific work directory via crontab schedule.
I found a way to do it in one line with env.
env -C workdir - command args

Deleting content of folder with shell script

Im having problems trying to empty a folder in my script.
This is working in my command line:
rm -r Folder1/Folder2/*
But if in my script I do this:
DIR="Folder1/Folder2/"
rm -r "$DIR*"
It says "rm: Folder1/Folder2/*: No such file or directory", where is the problem?
Im running the script in the same folder as I tried the command.
Glob expansion doesn't happen inside quotes.
Try:
rm -r -- "$DIR"*
(Just make really sure you don't put a space after the quotes.)
rm -r $DIR*
That should work, without quotes

Run text file as commands in Bash

If I have a text file with a separate command on each line how would I make terminal run each line as a command? I just don't want to have to copy and paste 1 line at a time. It doesn't HAVE to be a text file... It can be any kind of file that will work.
example.txt:
sudo command 1
sudo command 2
sudo command 3
you can make a shell script with those commands, and then chmod +x <scriptname.sh>, and then just run it by
./scriptname.sh
Its very simple to write a bash script
Mockup sh file:
#!/bin/sh
sudo command1
sudo command2
.
.
.
sudo commandn
you can also just run it with a shell, for example:
bash example.txt
sh example.txt
Execute
. example.txt
That does exactly what you ask for, without setting an executable flag on the file or running an extra bash instance.
For a detailed explanation see e.g. https://unix.stackexchange.com/questions/43882/what-is-the-difference-between-sourcing-or-source-and-executing-a-file-i
You can use something like this:
for i in `cat foo.txt`
do
sudo $i
done
Though if the commands have arguments (i.e. there is whitespace in the lines) you may have to monkey around with that a bit to protect the whitepace so that the whole string is seen by sudo as a command. But it gives you an idea on how to start.
cat /path/* | bash
OR
cat commands.txt | bash

bash - how to pipe result from the which command to cd

How could I pipe the result from a which command to cd?
This is what I am trying to do:
which oracle | cd
cd < which oracle
But none of them works.
Is there a way to achieve this (rather than copy/paste of course)?
Edit : on second thought, this command would fail, because the destination file is NOT a folder/directory.
So I am thinking and working out a better way to get rid of the trailing "/oracle" part now (sed or awk, or even Perl) :)
Edit :
Okay that's what I've got in the end:
cd `which oracle | sed 's/\/oracle//g'`
You use pipe in cases where the command expects parameters from the standard input. ( More on this ).
With cd command that is not the case. The directory is the command argument. In such case, you can use command substitution. Use backticks or $(...) to evaluate the command, store it into variable..
path=`which oracle`
echo $path # just for debug
cd $path
although it can be done in a much simpler way:
cd `which oracle`
or if your path has special characters
cd "`which oracle`"
or
cd $(which oracle)
which is equivalent to backtick notation, but is recommended (backticks can be confused with apostrophes)
.. but it looks like you want:
cd $(dirname $(which oracle))
(which shows you that you can use nesting easily)
$(...) (as well as backticks) work also in double-quoted strings, which helps when the result may eventually contain spaces..
cd "$(dirname "$(which oracle)")"
(Note that both outputs require a set of double quotes.)
With dirname to get the directory:
cd $(which oracle | xargs dirname)
EDIT: beware of paths containing spaces, see #anishpatel comment below
cd `which oracle`
Note those are backticks (generally the key to the left of 1 on a US keyboard)
OK, here a solution that uses correct quoting:
cd "$(dirname "$(which oracle)")"
Avoid backticks, they are less readable, and always quote process substitutions.
You don't need a pipe, you can do what you want using Bash parameter expansion!
Further tip: use "type -P" instead of the external "which" command if you are using Bash.
# test
touch /ls
chmod +x /ls
cmd='ls'
PATH=/:$PATH
if cmdpath="$(type -P "$cmd")" && cmdpath="${cmdpath%/*}" ; then
cd "${cmdpath:-/}" || { echo "Could not cd to: ${cmdpath:-/}"; exit 1; }
else
echo "No such program in PATH search directories: ${cmd}"
exit 1
fi
besides good answer above, one thing needs to mention is that cd is a shell builtin, which run in the same process other than new process like ls which is a command.
https://unix.stackexchange.com/questions/50022/why-cant-i-redirect-a-path-name-output-from-one-command-to-cd
http://en.wikipedia.org/wiki/Shell_builtin
In response to your edited question, you can strip off the name of the command using dirname:
cd $(dirname `which oracle`)

Resources