How to parametrize a variable value in shell script? - sh

I am a newbie to writing shell scripts. Please help me in parameterizing a variable value in my shell script.
I am taking command-line arguments for database name, server, user, and password in the following way:
database_name=$1
server=$2
user=$3
password=$4
I want to understand how I can pass these values to a variable called sqlcmd. I pass these values in the following way and then echo to see the value of sqlcmd variable:
sqlcmd=sqlcmd -S $server -U $user -P $password
echo $sqlcmd
after making the shell script executable using chmod a+x on ubuntu. I run the script and get the following error
line 37: -S: command not found. Line 37 in my shell script is a line on which sqlcmd variable is initialized
P.S I am using WSL on a remote windows machine. I am not sure if that should cause an error.

You need quotes:
sqlcmd="sqlcmd -S $server -U $user -P $password"
Note that you may run into difficulties later trying to execute the contents of sqlcmd.

Related

Execute command using plink.exe

I am trying to execute some command using plink.exe from powershell
below is the code which is working fine
plink.exe root#1.1.6.2 -pw $password -m "C:\command.txt" -no-antispoof
in command.txt, I have mentioned command as df -h
but when I tried to execute the command directly, it is not not returning any value.
plink.exe root#1.1.6.2 -pw $password "df -h" -no-antispoof
Please let me know what I am missing here. I have to execute multiple commands in various parts of my code. creating text file each command is getting difficult.
You cannot put plink switches after the command. This should work:
plink.exe root#1.1.6.2 -pw $password -no-antispoof "df -h"

plink with powershell commands not working

I having a bunch of Linux Servers on which i need to run few commands to get the host file entry of backup servers. I am using planning to make power shell script using plink to achieve this . It logs me in the server but the commands does not execute. Below is Powershell console
$switch = "172.20.19.50"
$commands = "c:\scripts\cmd.txt"
$username = root
$pw = Read-Host -Prompt "Enter password" -AsSecureString
plink -ssh $switch -l $username -pw $pw -m $commands
output
bash: Support: command not found
bash: Maintenance: command not found
my c:\scripts\cmd.txt contains following two commands :
Support
Maintenance
is there anything in command i am missing here ? Also if anyone could suggest a better way to get the output from linux servers remotely will be great help . I just need to query few commands to get the output
In 99.999% of all cases, if a computer tells you it cannot find something, the reason is, that that something is, in fact, not there. So, are you 100% sure that those commands exist on your server?
In the remaining cases, the reason is, that the thing is there, but not in the right place. Are you 100% sure that those commands are in root's $PATH?
I made a script wich does the same (connect to UNIX machine and execute command using Powershell) and I had to make this:
echo y | & $Plinkpath -P 22 -v $User#$server -pw $passw "$commands $($target)"
Where $commands are:
/usr/local/bin/sudo /usr/sbin/userdel
Because sudo and userdel was not always in mi $PATH (as said Jörg W Mittag) i've to use the full path to the command.
Hope this works for you!

Can't run commands on other user (su) using Plink in PowerShell

I'm trying to use Plink to connect to a remote UNIX server, su to another user (without password) and finally execute some commands.
$commands = #(
"su - $UNIXUSER;",
"id"
)
echo y | plink -ssh $SERVER -l $USER -pw $PWD $commands
When I execute the code above using PowerShell I get a message saying he was able to change the user, but when I execute the command id he returns the id I logged in in the first place, not the su user.
How can I execute commands using Plink within a su user?
This cannot work.
I'm surprised that you even get the id executed.
The PowerShell effectively executes this.
plink -ssh $SERVER -l $USER -pw $PWD "su - $UNIXUSER;" id
First that's a wrong syntax.
An even it were correct, you can provide only a single command string on plink command line. While you can combine multiple shell commands using ; or & to a simple command string, that cannot work with su. The second command, the ls is not command of the main shell anymore. That's an inner command of the su, i.e. a complete different stream/input/whatever you call it.
What you need to do is to emulate user typing the commands, so that the first command gets processed by the main shell and the second command by the su executed from the main shell. You can do that via an input redirection only.
You can do:
"su - $UNIXUSER`nid`nexit`nexit`n" | plink -ssh $SERVER -l $USER -pw $PWD -T
The -T was added to disable pty allocation (i.e. to get a non-interactive shell), to get the behavior of -m (which implies the -T).
(I do not do PowerShell. There's probably a more elegant way to present the commands than using the string.)

No Password Supplied / Positional Parameter not found PostgreSQL - Powershell

I am trying to build a simple script that outputs a query to a csv in powershell. However, it keeps returning the following error:
psql: fe_sendauth: no password supplied
I tried exposing the password as this will be a safe environment, but have seen suggestions to use pgpass but no real explanation on how to implement.
Set-Location 'E:\Program Files\PostgreSQL\9.1\bin\';
SET 'PGPASSWORD = myPwd';
.\psql -U postgres -w MyDatabase
copy {'SELECT * FROM table';} TO 'C:\Users\e\Desktop\test1.csv' CSV DELIMITER ',';
SET is an alias for Set-Variable, but Powershell variables are not environment variables. To set an environment variable, you need to use the $env: scope. Try:
$env:PGPASSWORD = 'myPwd';
See also here for more on environment variables.
Also, I don't think you can get away with putting raw input on the command line like that in PowerShell. I think it will treat things as separate commands, but I could be wrong.
You may also want to use the command switch (-c) and the PowerShell stop parsing symbol (--%) when you call psql to prevent PowerShell from parsing your command string:
.\psql --% -U postgres -w MyDatabase -c "copy {'SELECT * FROM table';} TO 'C:\Users\e\Desktop\test1.csv' CSV DELIMITER ',';"
Or set the commands to a variable with here-strings and pipe that to psql:
$query = #'
copy {'SELECT * FROM table';} TO 'C:\Users\e\Desktop\test1.csv' CSV DELIMITER ',';
'#
$query | .\psql -U postgres -w MyDatabase
Or about a dozen other ways to call an executable.

Out of memory exception when running data-only script

When I run data-only script in SQL Server 2008 R2, it is showing this error:
Cannot execute script
Additional information:
Exception of type 'System.OutOfMemoryException' was thrown. (mscorlib)
The size of script file is 115MB and it's only data .
When I open this script file, it shows:
Document contains one or more extremely long lines of text.
These lines cause the editor to respond slowly when you open the file .
Do you still want to open the file ?
I run schema-only script first and then data-only script .
Is there any way to fix this error ?
I solved it by using sqlcmd utitlity.
sqlcmd -S "Server\InstanceName" -U "instantName" -P "password" -i FilePathForScriptFile
For example :
sqlcmd -S .\SQLEXPRESS -U sa -P 123 -i D:\myScript.sql
Zey's answer was helpful for me, but for completion:
If you want to use Windows Authentication just omit the user and password.
And don't forget the quotes before and after the path if you have spaces.
sqlcmd -S .\SQLEXPRESS -i "C:\Users\Stack Overflow\Desktop\script.sql"
If you're logged into the domain with the correct privileges and there's only one instance running, you also do not have to provide the above user/pw/instance command args. I was able to just execute:
sqlcmd -i myfile.sql