can we execute shell script using gsutil command - shell

Option to execute shell script using gsutil is nowhere mentioned in the document. Tried some options but still no luck.I have a .sh file ,stored in a storage bucket,is there any way to execute this script using gsutil ?

gsutil doesn't support direct execution shell scripts, but you could pipe it to a shell, for example:
gsutil cat gs://your-bucket/your-script.sh | sh

Related

Running into error with BASH script with permission denied, but when running the command directly in bash shell its getting executed

When running below command directly in bash shell, I am able to get the output. But when I am passing it via BASH script getting access denied. Any help would be appreciated
$ jq -r '.id' Repooutput.txt
dad04f6d-4e06-4420-b0bc-cb2dcfee2dcf
Error:
$ sh test.sh
test.sh: line 3: /c/ProgramData/chocolatey/bin/jq: Permission denied
I think the reason is that when executing the script with sh test.sh we're asking the POSIX interpreter (shell) to execute the content on the script, while when executing it with ./test.sh we're asking the script to "execute itself".
For the latter, the file needs to have execution permissions, which you can add with
chmod +x test.sh
Issue was with the naming convention of JQ inside BASH folder path, because of which the script was unable to pick the command. Renaming the JQ within BASH folder resolved this

AWS CLI S3 cp Not Recognizing Quoted Source and Target in Shell Script

New to shell scripting. Trying to use shell script on RHEL 6.9 linux server to upload a file with whitespace in filename to AWS S3 with aws cli. I have tried single and double quotes and have been reading aws cli links like http://docs.aws.amazon.com/cli/latest/userguide/cli-using-param.html
Here is a simple version of my script with the problem:
#!/bin/bash
profile=" --profile XXXXXXX"
sourcefile=" '/home/my_login/data/batch4/Test File (1).zip'"
targetobject=" 's3://my-bucket/TestFolder/batch4/Test File (1).zip'"
service=" s3"
action=" cp"
encrypt=" --sse"
func="aws"
awsstring=$func$profile$service$action$sourcefile$targetobject$encrypt
echo $awsstring
$awsstring
When I run I get:
$ ./s3copy.sh
aws --profile XXXXXXX s3 cp '/home/my_login/data/batch4/Test File (1).zip' 's3://my-bucket/TestFolder/batch4/Test File (1).zip' --sse
Unknown options: (1).zip','s3://my-bucket/TestFolder/batch4/Test,File,(1).zip'
When I execute the $awsstring value from command line, it works:
$ aws --profile XXXXXXX s3 cp '/home/my_login/data/batch4/Test File (1).zip' 's3://my-bucket/TestFolder/batch4/Test File (1).zip' --sse
upload: data/batch4/Test File (1).zip to s3://my-bucket/TestFolder/batch4/Test File (1).zip
aws cli does not seem to recognize the quotes in the shell script. I need to quote the file names in my script, because I have white space in them.
Question: Why does the string execute correctly from the command line, but not from within the shell script?
Use eval $awsstring. I faced similar issue. You can look at my answer - https://stackoverflow.com/a/47111888/2396539
On second thought having space in file name is not desirable and if you can control it , avoid it in the 1st place.

sh: ...: is not an identifier when trying to invoke shell scripts using plink

Below is my shell script that I am trying to execute using PLINK on MachineB from MachineA(Windows Machine).
#!/bin/bash
export HIVE_OPTS="$HIVE_OPTS -hiveconf mapred.job.queue.name=hdmi-technology"
hive -S -e 'SELECT count(*) from testingtable1' > attachment22.txt
I am using plink to execute the shell script like below,
C:\PLINK>plink uname#MachineB -m test.sh
Using keyboard-interactive authentication.
Password:
Using keyboard-interactive authentication.
Your Kerberos password will expire in 73 days.
And this is the below error I always get whenever I try to run like above.
sh: HIVE_OPTS= -hiveconf mapred.job.queue.name=hdmi-technology: is not
an identifier
Something wrong with my shell script? or some trailing spaces? I am not able to figure it out. I am running PLINK from windows machine
The sh: prefix on the error message indicates that the script is being executed by sh, not bash.
bash lets you combine setting a variable and exporting it into a single command:
export foo=bar
sh, or at least some older versions of it, require these two actions to be separated:
foo=bar ; export foo
A version of sh that doesn't recognize the export foo=bar syntax will interpret the string foo=bar as a variable name (and an illegal one, since it isn't an identifier).
Either arrange for the script to be executed by bash, or change this:
export HIVE_OPTS="$HIVE_OPTS -hiveconf mapred.job.queue.name=hdmi-technology"
to this:
HIVE_OPTS="$HIVE_OPTS -hiveconf mapred.job.queue.name=hdmi-technology"
export HIVE_OPTS
For that matter, since you're referring to $HIVE_OPTS at the very beginning of your script, it's almost certainly already exported, so you could just drop the export.
(You'll also need to avoid any other bash-specific features.)
So why is the system invoking the shell with sh? The #!/bin/bash syntax is specific to Unix-like systems. Windows generally decides how to execute a script based on the file extension; apparently your system is configured to invoke *.sh files using sh. (You could configure your system, using Folder Options, to invoke *.sh files using bash, but that might introduce other problems.)
I think the -m option to plink is for reading commands to execute on the remote machine from a local file. If my comment about line endings doesn't work, try
plink uname#MachineB test.sh
Make sure test.sh is executable by running
chmod +x test.sh
on MachineB.

How to force ssh to execute bash instead of the user default on the remote machine?

I want to execute a bash script with ssh but when I try this it's using ksh which is the user's default shell.
I can't change that default.
So, how can I trick ssh to execute my script with bash instead of the default shell?
Make this the first line of your script:
#!/usr/bin/env bash
Edit: As per this, the utility of /usr/bin/env is dubious. So, you probably want:
#!/bin/bash
Replace /bin/bash with the actual path of bash executable.
You can call your script explicitly with bash:
ssh <ssh-opts> bash <scriptname>
This way there will be a ksh executed at login, but inside ksh you start a bash executing your script.

How do you execute a command on a remote system insde a BASH script?

As part of an intricate BASH script, I'd like to execute a command on a remote system from within the script itself.
Right now, I run the script which tailors files for the remote system and uploads them, then through a ssh login I execute a single command.
So for full marks:
How do I log into the remote system from the bash script (i.e. pass the credentials in non-interactively)?
How can I execute a command (specifically "chmod 755 /go && /go") from within the script?
Following Tim Post's answer:
Setup public keys and then you can do the following:
#!/bin/bash
ssh user#host "chmod 755 /go && /go"

Resources