Scripting in Linux and AWS CLI (command line interface) - linux

I am a newb to Linux and AWS and I'm trying to figure out what should be a simple task. I have an instance of Amazon Linux running in EC2 and I'm trying to write a script that will start some new instances (same AMI). The script (called my_script) is as follows:
#!/bin/sh
ec2-describe-instances
ec2-run-instances ami-b66ed3de -t t2.micro -k my-key-pair -g my_security_group
Whenever I try to run it, I get the error message:
./my_script: line 3: ec2-describe-instances: command not found
./my_script: line 4: ec2-run-instances: command not found
These commands work when I manually enter them in command line but not when I try to make a script containing them. I have tried fiddling around with the first line that says which interpreter to use, to no avail. Any input is appreciated. Thanks

Sometimes using commands in a script will not work, because the script is not run in the same environment as your current login. Try including the full paths to ec2-describe-instances and ec2-run-instances.
To find the full path, I believe you use the 'which' command in the command line.

I faced the same issue. I solved the issue by exporting the home directory at the beginning of the script. In my case, I added the following.
export HOME=/root

Related

Command not found in WSL2 even though it's on the path

I'm having an issue with WSL2:
$ where b4a
/usr/local/bin/b4a
$ b4a new
/usr/local/bin/b4a: 1: Not: not found
Even though where finds commands, I can't run them. And it's not a PATH issue either:
echo $PATH
/usr/local/sbin:/usr/local/bin:[...]
And b4a isn't the only command with this problem. What could be the cause? My distribution is Debian 10 and host is Windows 10.
Not necessarily a full answer, but hopefully the troubleshooting methods you need to arrive at a solution ...
Note that it doesn't say that the command itself isn't found. For instance, if you run:
# lllllllllll
lllllllllll: command not found
That's truly a command not found. This is different. While I don't (yet) know the exact cause, this seems closer to the issues we might see with improperly quoted paths with spaces in a shell script.
You mention that other commands have this problem -- Is there something in common with the commands that don't work properly? Is it possible that they are all shell scripts?
Try several things to debug:
Start WSL without your startup profile. It's very likely that something (or you) added a line that is causing problems. From PowerShell or CMD:
wsl ~ -e bash --noprofile --norc
b4a
If that works, then there's a problem in one of your startup files that you'll need to debug. Look for anything modifying environment variables without proper quoting, especially the PATH. WSL automatically appends the Windows path to your Linux path to make it easy to run Windows commands, but the fact that almost every Windows path has spaces in it can cause problems for unsuspecting scripters that don't take this corner case into account.
Having a space in a path is fully allowed in Linux, but some scripts just don't handle it properly.
If the command that is failing is a shell script, trying starting it with:
bash -x /usr/local/bin/b4a
Or even start WSL with wsl ~ -e bash -x to see all trace output from the shell.
In this case, you'll be looking for some problem in the script right around where it actually fails.
If all else fails, you can disable WSL's PATH modification via its config file:
sudo -e /etc/wsl.conf
Add the following:
[interop]
appendWindowsPath = false
Then exit Debian, run wsl --shutdown and restart Debian. Try the b4a command again.
If this works, then the problem is almost certainly due to some problem in the PATH quoting in these commands. I don't recommend it as a permanent solution since you will have to type out the full path of Windows applications each time you want to run them.

Unable to execute shell script using deploy command in jboss

I have been looking for solutions to this problem for a couple of days and have not found a suitable one.
Currently i have two lines of code in my shell script file.
cd /app/jboss/sample_project/bin
./jboss-cli.sh -c --controller=$hostnameVal:$jboss_port_no1 --user=$jboss_id --password=$jboss_pwd --command="deploy --force /sample/uploaded/sample-1.0.war --runtime-name=sample-1.0.war"
I am facing this error:
'--force' is assumed to be a command(s) but the commands to execute have been specified by another argument: [deploy]
I think it has something to do with the spaces between deploy and --force but I can't seem to find a solution to this. Would really appreciate if someone can shed some light. Thank you!
The root cause of the issue was due to the space between the deploy command and the file path, therefore jboss was not be able to find the parameter the properly.
Hence, a work around for this which I have found out is to create a cli file, write the deploy command in the cli file and pass the file as an input in the script that i am writing.
Do you observe the same problem in a terminal?

Other ways for auto-start script in Kali Linux?

So I'm basically wanting to get a script to run on system boot. It's basically an SSH callback. I've tried a few ways that I've gotten to work in the past on other distributions, but having a little bit of difficulty here.
First thing I've tried was adding the /path/to/script.rb to /etc/rc.local. However, this file does not exist on the latest version of Kali Linux. I tried to create it and replicate my old Ubuntu rc.local file, but it didn't run on system startup.
Next thing I tried was creating an executable bash script in /etc/init.d/, following the update-rc.d script.sh defaults and making the file executable. Restarted and nothing. If I run the script manually, it works. I tried to redirect the output to a file in the tmp folder, but it doesn't appear that there are any errors from what I'm understanding.
Are there any other ways to get an auto run script started other than these two methods? Seem to be the most common way to get this working, but it's just not doing it for me.
Add script to
/etc/init.d
Run command:
chmod 755 /etc/init.d/script
Run command:
update-rc.d script enable

ct: not yet implemented

In Linux when I try to run ct setview inside shell script I am getting ct: not yet implemented error but if I run same command in command line I am able to run the command.
Can anyone please help me resolving this issue.
Follow some tutorials like this it appears that ct is an alias for /usr/atria/bin/cleartool and not a command.
Aliases are only available in interactive shells, not in shell scripts.
Use:
/usr/atria/bin/cleartool
instead of
ct
in your script.

sh file not found in linux command line

I'm an owner of synology's diskstation NAS server running on special OS (mainly linux)
It has only access from internet. I established command line interface to access it using synology wiki. Now I try to install Counter-Strike server on it using SteamCMD. I just look in Valve Developer Community for it.
I'm writing the next:
mkdir csServer
cd csServer
wget http://media.steampowered.com/client/steamcmd_linux.tar.gz
tar xvfz steamcmd_linux.tar.gz
./steamcmd.sh
But the last line causes -sh: ./steamcmd.sh not found. However, I can see this file in the directory.
What actually can cause it?
//ADDED
The file is executable!
Your script contains a header which says #!/bin/bash, that is use /bin/bash to execute this script. Apparently, on your system /bin/bash is not present. I am not familiar with Synology environment, but you should be able to fix this problem by installing bash and then pointing your script to the right path, which should be /opt/bin/bash. The first line of your script should therefore look like the following
#!/opt/bin/bash
You could also use Synology default ash, but beware that you script might not work as it was written for bash.
Windows style end-of-lines (CRLF) can cause this issue. If your sh file contains Windows style end-of-lines, convert it to Linux style.

Resources