Bash script: su another user and ssh in the single-command
Hey guys, this is my first post to this forum, i heard good things about it, and its one of the best sites to get help nowdays :)
i have this problem i wrote a large post about, but gotten a "bad gateway error, so i have to rewrite it all again :( well thats why it looks a little short, as i kinda got anoyed by that :/ here is my problem, i have a bash script i want to do ssh with another user, so i use the Code:
#!/usr/local/bin/bash hope i didnt sound too impolite, i am realy a nice guy :P |
Hello,
What part of it doesn't work? The first thing I have noticed is the very first line; I know on most systems, bash is installed to /bin/bash. What distro are you running? Cheers, Josh |
Quote:
as i outcommendted, it is FreeBSD 8.1. but it seems when i try to use a mysql command is gives an mysql error wit the No databases by that name: database as it seems to cut of the "Show databases" with "show" and then adds Databases as the database to use. so it simply breaks my Quotes :( i tryed with \\\"Show databases\\\" seems to just open a whole other can of worms |
Oops, didn't even notice.....
As far as the message you are getting about the database, I would run it by itself just to double check the format, and to make sure that it works before using it within your script. You could also store it within a variable so it doesn't complain about too many quotation marks. |
it works like this
Code:
#!/usr/local/bin/bash |
You could always use single quotes for the actual command;
Code:
ssh_cmd 'mysql -u${MYSQLUSER} -p${MYSQLPASS} -Bse "SHOW DATABASES"' |
i tried playing around with single quotes, it seems to give problems with variables, so i outquoted them
ssh_cmd 'mysql -u'"${MYSQLUSER}"' -p'"${MYSQLPASS}"' -Bse "SHOW DATABASES"' seems it cuts them off, even when i try echoing from my functions, and the come out all fine.. i guess the main problem is the switch from as_user <-> ssh_cmd as the command first gets evaluated from as_user.. hmm im still trying to dig down, but maby i am just doing it wrong, and a person already found a better way to do it like this? :P actually why i am doing this, is becasue i need to access a users id_dsa file for private ssh connection.. however i figured i could just get it from ssh by adding -i /home/${USER}/.ssh/id_dsa to my ssh command.. it works better.. but still i wanted to get my as_user function to work for later use. |
yay, i got it working.. the problem was the "bash -c" can't work within another with double-quotes, it have to be single-quotes.. as my full string is assembled by that point, it doesn't matter anyway.. so my fix was this.
Code:
dtest="echo \"Hello World\"" thanks for the help corp769 :) |
No problem! And sorry I didn't see the notification at first, I got kind of busy here at work, and overlooked it. Glad to see you got it working!
Cheers, Josh |
you only need one snowflake to start an avalanche, and i guess your help where all i needed.
gl at work. i better work too, now that i got that solved |
All times are GMT -5. The time now is 08:55 AM. |