I have a bash script like so:
#!/bin/bash
cd /
pwd
ls
cd Users
pwd
ls
cd robl
pwd
ls
cd Documents
pwd
ls
When I run it manually via ~/bin/script.sh, it works as expected.
However, when I make a .plist, schedule it and start it it using launchctl like so:
launchctl load ~/Library/LaunchAgents/com.robl.script
launchctl start com.robl.script
It fails. Specifically, it runs as expected until the last ls, on which it throws the error
ls: .: command not found
I think this may be some kind of permission issue since the script works when running manually but not via launchctl. Especially because all of the ls‘s work and print the contents, except for the last one in the Documents folder.
Any ideas?
Related
I am trying to execute the following user data script
sudo wget https://files.mysite.com/downloads/myFile.tar -P /opt
sudo tar -xvf /opt/myTar.tar -C /opt
sudo /opt/myFile.sh
When I execute the .sh file manually I see this:
Extracting...
Unpacking...
Cleaning up...
Complete
and it creates a directory in /opt/myDirectory
I do see the console in /var/log/cloud-init-output.sh but it doesn't seem to create the directory when run as part of the userdata script.
You're calling a shell script within a shell script, so you need to make sure:
have this as the first line of your ec2 user data script #!/bin/bash
call myFile.sh with the source command (alias is .) like this: . /opt/myFile.sh so it will run the myFile script
Note: the ec2 user data script runs as root so you do not need to have sudo each time you run a command.
Solution: CD into the directory first.
#!/bin/bash
cd /opt
wget https://example.com/myTAR.tar
tar -xvf myTAR.tar
/opt/mySH.sh
I have a user named cam. Cam stores a bunch of files. Now I want to move those files so I tried the following...
sudo mv /home/cam/DCS-*.jpg /home/cam/cam/
But when I run this command I get...
mv: cannot stat ‘/home/cam/DCS-*.jpg’: No such file or directory
But if I runt the command like...
sudo mv /home/cam/DCS-934L2015110711425501.jpg /home/cam/cam/
It works fine. WTF am I missing
if I do a sudo ls /home/cam I see everything but without sudo I don't have permissions to see anything.
When this command is executed:
sudo mv /home/cam/DCS-*.jpg /home/cam/cam/
The * is expanded by the shell according to the permissions of the current user. As the current user cannot see those files (ls /home/cam has no permission), the shell cannot expand the parameter list.
shouldn't sudo have permissions regardless?
No. With sudo, the mv command will be executed as root, but the parameter list expansion happens before execution is passed to sudo mv.
To have the * expansion happen with root permission (so that the content of the directory will be visible), you can wrap the command in its own shell like this:
sudo sh -c 'mv /home/cam/DCS-*.jpg /home/cam/cam/'
I have a script(func_test) that works well when i invoke it from my terminal. I need to run the script automatically on boot-up and so i have copied it in /etc/init.d and changed its execution mode and linked it to S99func_test under /etc/rc2.d. But upon reboot I'm getting syntax error in that script. Any idea why I'm getting the error although it works fine with my terminal?
Here is the code used to invoke the script for the 1st time:
#!/bin/bash
cd /opt/bin/
cp func_test /etc/init.d/
cp test_file /etc/init.d
chmod 755 /etc/init.d/func_test
chown root:sys /etc/init.d/func_test
ln /etc/init.d/func_test /etc/rc2.d/S99func_test
ln /etc/init.d/test_file /etc/rc2.d/S99test_file
(the script is dependent on another file(test_file) and i have copied the same to init.d)
Probably you forgot slash at the end of line
cp test_file /etc/init.d/
and I recommend to use ./ to copy files from current directory:
cp ./func_test /etc/init.d/
I was trying the below program,
This is a simple script, to cd into a folder
#! /bin/bash
cd /root/
But this below command , doesnt get into the folder
EDITED
#!/bin/bash
alias ex="cd /fs/fm"
alias ex1="source setenv"
alias ex2="cd /fs/fm/tests"
alias ex3="runtest"
To get into /root/ you should make sure that you have permissions. It's accessible if you're running as root itself but if you're running as a normal user you should consider becoming root first. One way is to use sudo:
sudo bash script.sh
And again, make sure your script is in UNIX format. Certainly you can't change to /root/\r.
sed -i 's|\r||' script.sh
dos2unix script.sh
This will never work The script you're running is a separate process, when it finishes you get back to the original environment (cwd, enviroment variables, etc...).
Create an alias:
alias r="cd /root"
or execute the script within your shell:
. myscript
Note: . is a synonym for source.
I have searched on how to run a script file on Mac but nothing works for me.
Every time I tried sudo script-name the terminal responds with
-bash: /Users/macuser/Desktop/tesseract-3.01: is a directory
The file I want to run is called start and it's located in tesseract-3.01 directory on the desktop.
simply do
/Users/macuser/Desktop/tesseract-3.01/start
or if it's actually called start.sh
/Users/macuser/Desktop/tesseract-3.01/start.sh
you might also want to do
chmod +x /Users/macuser/Desktop/tesseract-3.01/start.sh
to change the script to be executable before you run the script
sudo /Users/macuser/Desktop/tesseract-3.01/start
You have to indicate the script name, but it looks like you were only specifying the directory.
You could also cd to the directory and then run it like so:
cd /Users/macuser/Desktop/tesseract-3.01
sudo ./start
Try
sudo ./Users/macuser/Desktop/tesseract-3.01/start.sh
or
cd /Users/macuser/Desktop/tesseract-3.01
then
sudo ./start.sh