I am trying to run .sh file from cygwin on windows 7
My dumdb.sh file content
#!/bin/bash
for database in $(mysql -e "show databases"|awk -F " " '{print $1}') do
mysqldump -u root -h localhost -p $database > $database.sql
done
On running this command
$ sh dumpdb.sh
m getting following error
bash: line 3: syntax error near unexpected token `mysqldump'
bash: line 3: `mysqldump $database > $database.sql'
Where I am doing wrong?
You are missing a ; before do:
#!/bin/bash
for database in $(mysql -e "show databases"|awk -F " " '{print $1}') ; do
mysqldump $database > $database.sql
done
Related
I am trying to create a pipeline in gitlab-ci. my problem is that when I run ls command I can see my files, but when I use for condition, I cannot find same files. any suggestion?
script:
- set -vux
- ls ./dist/*
- |
for file in ./dist/*; do
if [ -f ${file} ]; then
export ARTIFACTORY_IMAGE_TAG=${DATASCIENCE_REPO}/$(basename ${file})/$IMAGE_VERSION
'curl -H "X-JFrog-Art-Api: $ARTIFACTORY_PW" -XPUT "$ARTIFACTORY_IMAGE_TAG" -T dist/$file'
fi
done
dependencies:
- package-build
my output is like this:
$ ls ./dist/*
ls ./dist/*
++ ls ./dist/__init__.py ./dist/my_proj-0.0.1.1164649-py3-none-any.whl ./dist/file1.py ./dist/file2.py ./dist/file3.json ./dist/file4.json
./dist/__init__.py
./dist/my_proj-0.0.1.1164649-py3-none-any.whl
./dist/file1.py
./dist/file2.py
./dist/file3.json
./dist/file4.json
echo $'\x1b[32;1m$ for file in ./dist/*; do # collapsed multi-line command\x1b[0;m'
++ echo '$ for file in ./dist/*; do # collapsed multi-line command'
$ for file in ./dist/*; do # collapsed multi-line command
for file in ./dist/*; do
if [ -f ${file} ]; then
export ARTIFACTORY_IMAGE_TAG=${MY_REPO}/$(basename ${file})/$IMAGE_VERSION
'curl -H "X-JFrog-Art-Api: $ARTIFACTORY_PW" -XPUT "$ARTIFACTORY_IMAGE_TAG" -T dist/$file'
fi
done
++ for file in ./dist/*
++ '[' -f ./dist/__init__.py ']'
+++ basename ./dist/__init__.py
++ export ARTIFACTORY_IMAGE_TAG=https://artifactory.xyz.com/artifactory/my_proj_dev/__init__.py/0.0.1.1164649
++ ARTIFACTORY_IMAGE_TAG=https://artifactory.xyz.com/artifactory/my_proj_dev/__init__.py/0.0.1.1164649
++ 'curl -H "X-JFrog-Art-Api: $ARTIFACTORY_PW" -XPUT "$ARTIFACTORY_IMAGE_TAG" -T dist/$file'
/scripts-11718-6374270/step_script: line 239: curl -H "X-JFrog-Art-Api: $ARTIFACTORY_PW" -XPUT "$ARTIFACTORY_IMAGE_TAG" -T dist/$file: No such file or directory
Cleaning up project directory and file based variables
00:00
ERROR: Job failed: command terminated with exit code 1
Update 1: according to the error I am receiving, I think in my curl command system cannot file the file. but why I am not sure?
Simple syntax mistake.
The error says there is No such file or directory.
line 239: curl -H "X-JFrog-Art-Api: $ARTIFACTORY_PW" -XPUT "$ARTIFACTORY_IMAGE_TAG" -T dist/$file: No such file or directory
An experiment.
Let's run these three commands in your terminal.
The first one
(DO NOT REMOVE A SINGLE QUOTE.)
'touch love and peace'
The result is in bash
-bash: touch love and peace: command not found
The result is in zsh
zsh: command not found: touch love and peace
A terminal recognizes the single-quoted string as a command in the first command.
The second one
(DO NOT REMOVE A SINGLE QUOTE.)
'touch /'
The result is in bash
-bash: touch /: No such file or directory
The result is in zsh
zsh: no such file or directory: touch /
A terminal recognizes the single-quoted string with the slash as a file in the second command.
The third one
without a single quote.
touch love and peace
The result is
$ls
and love peace
If you intend to run the curl command in every loops,
Just remove the single quotes.
curl -H "X-JFrog-Art-Api: $ARTIFACTORY_PW" -XPUT "$ARTIFACTORY_IMAGE_TAG" -T $file
The solution
for file in ./dist/*; do
if [ -f ${file} ]; then
export ARTIFACTORY_IMAGE_TAG=${DATASCIENCE_REPO}/$(basename ${file})/$IMAGE_VERSION
curl -H "X-JFrog-Art-Api: $ARTIFACTORY_PW" -XPUT "$ARTIFACTORY_IMAGE_TAG" -T $file
fi
done
It seems like the problem is that you are not listing the files in your for statement. Try:
for file in `ls ./dist/*`; do
echo "act on $file"
done
I am trying to create a script that will automatically log me in to a specific remote device (let's call it a fw). The "command" is a bit elaborate, as we are logging in from a protected network server, and there are hundreds of these to login to.
I have created a file with two parameters (command and name) separated by "#", the first parameter is the "command" string with spaces (ie: "sudo --user ....") which I want to put (echo) into an executeable file called "name" (name of the device I want to login to).
My logic was originally:
for line in $(awk -F# '{print $1, $2}' list.txt), do touch $2; && echo "$1 > $2" && chmod +x $2; done
my end should create x number of files named "$name", each with only a one-line command of "$command" and be "executeable".
I have tried several things to make this work. I can iterate of the file with not much issue using for, while, and even [[ -n $name ]], but, this only provides me with one variable and doesn't split the line into the two I need, "$command" and "$name". Even $1 and $2 would be fine for my purposes...
While testing:
$ while IFS=# read -r line; do echo "$line"; done < list
sudo --user xxxxxxxxxxxxxx#yyyyyyyyy
sudo --user xxxxxxxxxxxxxx#yyyyyyyyy
sudo --user xxxxxxxxxxxxxx#yyyyyyyyy
even using IFS=# to split the $line - doesn't remove the "#" as expected.
for-looping:
$ for line in $(cat list); do echo $line; done
sudo --user xxxxxxxxxxxxxx
yyyyyyyyy
sudo --user xxxxxxxxxxxxxx
yyyyyyyyy
sudo --user xxxxxxxxxxxxxx
yyyyyyyyy
Trying to expand to:
bin$ for line in $(cat list); do awk -F# '{print $1, $2}' $line; done
awk: fatal: cannot open file ` xxxxxxxxxxxxxxxxx' for reading (No such file or directory)
awk: fatal: cannot open file `yyyyyyyyy
sudo --user xxxxxxxxxxxxxxxxx' for reading (No such file or directory)
awk: fatal: cannot open file `yyyyyyyyy
sudo --user xxxxxxxxxxxxxxxxx' for reading (No such file or directory)
I would like to parse (loop) through the file - separate the parms and create $name with $command inside and chmod +x $name to have an executeable that will log me in automatically to "$name" node.
I suggest inserting all your logic into the awk script.
script.awk
BEGIN {FS = "[\r#]"} # set field separator to # or <CR>
{ # for each input line
print $1 > $2; # write input 1st field to file named 2nd field
system("chmod a+x "$2); # set file named 2nd field, to be executable
}
running the script:
awk -f script.awk list.txt
input list.txt
sudo --user xxxxxxxxxxxxxx#yyyyyyyy1
sudo --user xxxxxxxxxxxxxx#yyyyyyyy2
sudo --user xxxxxxxxxxxxxx#yyyyyyyy3
output:
dudi#DM-840$ ls -l yy*
total 3
-rwxrwxrwx 1 dudi dudi 28 Jun 23 01:21 yyyyyyyy1*
-rwxrwxrwx 1 dudi dudi 28 Jun 23 01:21 yyyyyyyy2*
-rwxrwxrwx 1 dudi dudi 28 Jun 23 01:21 yyyyyyyy3*
update:
changed FS to include the <CR> char, otherwise appended to filenames (seen as ^M).
I'm getting 7: Syntax error: "(" unexpected error while running bellow code on Ubuntu. But It's run on centos without any issues.
#!/bin/sh
#
TODATE=`date '+%Y-%b-%d'`
#
# Backup Creation for Databases
#
databases=(`echo 'show databases;' | mysql -u root -ppaSSword | grep -v ^Database$`)
for DB in "${databases[#]}"; do
mysqldump --force --opt --user=root --password=paSSword $DB | gzip > /mnt/Backup/DB/${DB}_${TODATE}.sql.gz
done
#
Please help me to solve this.
I can't figure out problem. But,
I'm using bellow code for backup. It's working fine with Ubuntu
#!/bin/bash
#
TODATE=`date '+%Y-%b-%d'`
databases="$(mysql -u root -ppaSSword -Bse 'show databases')"
for DB in $databases
do
mysqldump -u root -psqlMYadmin $DB | gzip > /mnt/Backup/DB/${DB}_${TODATE}.sql.gz
done
You can redirect the 'show databases' output to dump.txt file, if done then try.
#!/bin/bash
da=$(date +"%d-%m-%y")
for db in `cat dump.txt` ; do mysqldump --force --opt --user=root --password=paSSword $db | gzip /path/to/backup/$db_"$da".sql.gz ; done
You need to escape the last '$' on the line databases= ...
There's only one ( in the script and you have the shebang line #!/bin/sh. My best guess is that the program /bin/sh does not recognize array assignment, whereas /bin/bash would.
Change your shebang to #!/bin/bash.
You'd probably do better to use $(...) in place of the back ticks.) Also, as Sami Laine points out in his answer, it would be better if you quoted the regex to the grep command (though it is not the cause of your problem):
databases=( $(echo 'show databases;' | mysql -u root -ppaSSword | grep -v '^Database$') )
I'm creating a server in Amazon ec2 and passing it a bash script as userdata, which is run when the server first boots. It includes a command to add a line to crontab for a user using the answer given here.
directory="/home/intahwebz/current/tools/amazon/"
command="cd $directory && sh backupSQLToS3.sh"
job="15 1 */2 * * $command"
cat <(fgrep -i -v "$command" <(crontab -u intahwebz -l)) <(echo "$job") | crontab -u intahwebz -
This script appears to work fine during bootup as it displays no error messages and the cronjob is installed in the crotab.
However I'd also like the script to run during server upgrades. Attempting to run the script from the command line gives the error:
installCrontab.sh: line 14: syntax error near unexpected token `('
installCrontab.sh: line 14: `cat <(fgrep -i -v "$command" <(crontab -u intahwebz -l)) <(echo "$job") | crontab -u intahwebz -'
What do I need to fix this error?
your approach is working perfectly for me:
$ whoami
test
$ echo $SHELL
/bin/bash
$ command="cd $directory && sh backupSQLToS3.sh"
$ job="15 1 */2 * * $command"
$ crontab -l
$ cat <(fgrep -i -v "$command" <(crontab -u test -l)) <(echo "$job") | crontab -u test -
$ crontab -l
15 1 */2 * * cd && sh backupSQLToS3.sh
I missed to set the "directory" variable but your code works fine for me.
It looks like you are using the bourne shell (/bin/sh) to execute a bash script. Try using bash instead of sh.
The code:
`cat <(fgrep -i -v "$DAEMON_TEST" <(sudo -u asm crontab -l)) <(echo "$CRON") | sudo -u asm crontab -`
The error:
command substitution: line 46: syntax error near unexpected token `('
/etc/init.d/asm: command substitution: line 46: `cat <(fgrep -i -v "$DAEMON_TEST" <(sudo -u asm crontab -l)) <(echo "$CRON") | sudo -u asm crontab -'
The command runs fine when run directly into the shell by replacing the variables with the relevant strings
Here are the variables:
DAEMON_TEST=asm_test.php
CRON="*/15 * * * * /opt/asm/daemons/test.php"
The issue ended up being completely unrelated.
Changed:
#/bin/sh
to:
#/bin/bash
I never realized there would be a differnce
https://superuser.com/questions/125728/what-is-the-difference-between-bash-and-sh