Shell script: Command not found - bash

I'm scripting a file which will change the permissions of a file once it is run. I keep getting these errors on the Cygwin terminal:
$ chmx.bash
/home/user/scripts/chmx.bash: line 2: $'\r': command not found
/home/user/scripts/chmx.bash: line 4: $'\r': command not found
chmod: cannot access ‘chmxtext.txt\r\r’: No such file or directory
Here's my script:
#!/bin/bash
fileName="chmxtext.txt"
chmod a+rwx "$fileName"

Related

-bash: ‘export: command not found -bash: 400:: command not found both appear on mac terminal

Both appear when I open up the terminal.
I tried to get git autocompletion by following these instructions:
go to terminal
put the git-completion.bash script in your home directory:
curl https://raw.githubusercontent.com/git... -o ~/.git-completion.bash
Add the following line to your: .bash_profile; this tells bash to run the git autocomplete script if it exists
vi ~/.bash_profile
if [ -f ~/.git-completion.bash ]; then
. ~/.git-completion.bash
fi
But it didn't work, and now I get these two lines when I open the terminal and they won't go away:
-bash: ‘export: command not found
-bash: 400:: command not found

Bash, excluding some file from the result of ls command, only by using the path

I would like to exclude some files from the output of an ls command.
As an example, I have these files :
[toto#damageinc ~]# ls -drt1 test_dir/*.txt
test_dir/FIXED-PATTERN1-RANDOM.txt
test_dir/FIXED-PATTERN2-RANDOM.txt
test_dir/FIXED-PATTERN3-RANDOM.txt
test_dir/FIXED-PATTERN4-RANDOM.txt
test_dir/FIXED-PATTERN5-RANDOM.txt
I want to exclude files containing PATTERN2 and PATTERN3 (extended globbing is set)
[toto#damageinc ~]# ls -drt1 test_dir/FIXED-!(PATTERN2|PATTERN3)-*.txt
test_dir/FIXED-PATTERN1-RANDOM.txt
test_dir/FIXED-PATTERN4-RANDOM.txt
test_dir/FIXED-PATTERN5-RANDOM.txt
Works fine.
The problem is that I do not execute the ls command myself and I cannot modify the way it is launched : sh -c "ls -drt1 path".
I just have access to "path", passed as a parameter to an executable (binary) containing this sh -c command
When I give the executable the path "test_dir/FIXED-!(PATTERN2|PATTERN3)-*.txt", I have this error :
sh: -c: line 0: syntax error near unexpected token `('
sh: -c: line 0: `ls -drt1 test_dir/FIXED-!(PATTERN2|PATTERN3)-*.txt'
Which is exactly the same when doing this by myself in the console :
[toto#damageinc ~]# sh -c 'ls -drt1 test_dir/FIXED-!(PATTERN2|PATTERN3)-*.txt'
sh: -c: line 0: syntax error near unexpected token `('
sh: -c: line 0: `ls -drt1 test_dir/FIXED-!(PATTERN2|PATTERN3)-*.txt'
How can have the correct result via sh -c, by modifying only my expression :
test_dir/FIXED-!(PATTERN2|PATTERN3)-*.txt
Thanks

mv: cannot move file to '': No such file or directory in bash

I have following command
mv 15827.png "$(<15827.png.txt)"
Which is moving the file 18827.png to the path specified in 15827.png.txt, and it is working fine.
But when I moved this command to shell script
#!/bin/bash
mv 15827.png "$(<15827.png.txt)"
I'm running it with:
sh myscript.sh
Its not working and I am getting following error:
mv: cannot move '15827.png' to '': No such file or directory
The file 15827.png.txt contains the digit 7 and there is folder named 7 in the current directory.
The problem is that you're running the script with sh, but it needs to be run with bash, because $(<filename) is a bash extension.
Make the script executable:
chmod 755 myscript.sh
and then run it with:
./myscript.sh
This will execute the script using the shell named in the #! line.

File exists but showing no such file or directory

I am trying to perform a set of terminal operations using bash shell script. Below is my code
#!/bin/bash
FILE_DATE=`date '+%Y%m%d'`
ARCHIVE_DIR="/home/tanmay/backup/"
TAR_GZ=".tar.gz"
PATH=( "/home/tanmay/Downloads/apache-tomcat-7.0.69/logs" "/home/tanmay/Downloads/apache-tomcat-7.0.69/webapps" )
FOLDER=("logs" "webapps" )
for number in {0..1..1}
do
echo ${PATH[number]}
echo ${FOLDER[number]}
rsync -vrzh ${PATH[number]} ${ARCHIVE_DIR}
tar -zcvf ${ARCHIVE_DIR}/${FOLDER[number]}${TAR_GZ} ${ARCHIVE_DIR}/${FOLDER[number]}
rm -rf ${ARCHIVE_DIR}/${FOLDER[number]}
if [ -f ${ARCHIVE_DIR}/${FOLDER[number]}${TAR_GZ} ]
then
mv ${ARCHIVE_DIR}${FOLDER[number]}${TAR_GZ} ${ARCHIVE_DIR}${FOLDER[number]}_${FILE_DATE}${TAR_GZ}
fi
done
When I run this script, both the echo is showing the correct values. But operations (rsync,tar..) are returning file not found. Below is the output
/home/tanmay/Downloads/apache-tomcat-7.0.69/logs
logs
./server_data_backup_updated.sh: line 11: rsync: No such file or directory
./server_data_backup_updated.sh: line 12: tar: No such file or directory
./server_data_backup_updated.sh: line 13: rm: No such file or directory
/home/tanmay/Downloads/apache-tomcat-7.0.69/webapps
webapps
./server_data_backup_updated.sh: line 11: rsync: No such file or directory
./server_data_backup_updated.sh: line 12: tar: No such file or directory
./server_data_backup_updated.sh: line 13: rm: No such file or directory
UPDATE 1
Using one array instead on two. It is working now.
#!/bin/bash
FILE_DATE=`date '+%Y%m%d'`
ARCHIVE_DIR="/home/tanmay/backup/"
TAR_GZ=".tar.gz"
array=( "/home/tanmay/Downloads/apache-tomcat-7.0.69/logs"
"logs"
"/home/tanmay/Downloads/apache-tomcat-7.0.69/webapps"
"webapps")
for number in {0..2..2}
do
rsync -vrzh ${array[number]} ${ARCHIVE_DIR}
tar -zcvf ${ARCHIVE_DIR}/${array[number+1]}${TAR_GZ} ${ARCHIVE_DIR}/${array[number+1]}
rm -rf ${ARCHIVE_DIR}/${array[number+1]}
if [ -f ${ARCHIVE_DIR}/${array[number+1]}${TAR_GZ} ]
then
mv ${ARCHIVE_DIR}${array[number+1]}${TAR_GZ} ${ARCHIVE_DIR}${array[number+1]}_${FILE_DATE}${TAR_GZ}
fi
done
When you reset PATH, the shell can no longer find the executable rsync. When the shell reads the word rsync, it looks through the variable named PATH (which it expects to be a colon separated list of directories, not an array) for a file named rsync. Similarly for tar and rm. The error messages you see are simply telling you that those commands are not found in your PATH.

Missing matching quote with su -c in bash script

I want to write something like
EXEC="sudo su -m root -c \"java Something\""
$EXEC &
But i get the following error:
Something": -c: line 0: unexpected EOF while looking for matching `"'
Something": -c: line 1: syntax error: unexpected end of file
If I write the command on the command line it executes. If I have it stored in a variable and trying to extrapolate it - it does not. Why?
Try this:
exec="ls -l \"/a b c\""
$exec
You will see something like:
ls: cannot access "/a: No such file or directory
ls: cannot access b: No such file or directory
ls: cannot access c": No such file or directory
Which shows exactly where the problem is - that is - expansion of variable is done after word splitting.
To make it work, you can use eval:
=$ eval "$exec"
ls: cannot access /a b c: No such file or directory
or even:
=$ sh -c "$exec"
Or better yet, don't make such commands to run. Not sure what is the purpose of it, but think about avoiding building full command lines in variables.

Resources