How do I access a share drive in a cygwin bash script? - bash

#!/bin/bash
"mirror -R //thevault/Shared/Operations/Marketing/test --only-missing -e ;exit"
I'm using lftp to mirror a folder on a share drive. But it fails with "No such file or directory" What's the correct way to write this? and How would I escape a space in the file name. I've tried literally everything.
I've also tried /cygdrive/s/Operations/Marketing/test. This works while I'm logged in and I run the script. But when the task is run while I'm not logged in the log file I get the same "No such file" Error.

I don't think that is support in general in cygwin for UNC pathnames (ie \\server\share) so you'll have to rely on mapping to a network drive and then using /cygdrive/s. To fix the problem with it not working when you aren't logged in, you'll need to call the Windows NET program from your script:
net use s: \\thevault\Shared password /user:myuser
There may be some security implications to having the password in plaintext, so another possibility is to ensure that the script is running from a user account that has read permission to this server, and then you can omit the password.

This seems to be working perfectly while logged out.
#!/bin/bash
lftp ftp://username:password#server.com -e "mirror -R //thevault/Share/folder/folder/folder\ with\ spaces/folder/folder --only-missing -e;exit"
It was the escaped spaces in my path. The reason that this didn't work is because when I retyped the path I misspelled share. //thevault/shared <~~ incorrect
#!/bin/bash
"mirror -R //thevault/Shared/folder/folder/test --only-missing -e ;exit"

Related

Batch script permissions issues

I have a batch script written to auto start and capture traffic on a server for me but for some reason when I run it wireshark tells me it doesn't have permission to the folder where the script is trying to save the file. I have tried multiple different folders on and off the server I have tried giving everyone including SYSTEM full access to the folder. I have tried remaking the folder. I have tried running under and not under admin credentials I have tried letting the system task run it. Always get a permissions issue.
The weirdest part is if I run wireshark manually and save the data manually it has no permissions issues. Just if I run the script is the problem. Although they're both run under the same admin account.
Here is the script in case you need to see the flags I used.
#echo off
cd C:\Program Files\Wireshark
Wireshark.exe -i 4 -k -a duration:10 -w C:\Temp
pause
I did try to use a powershell script I had found online but it was pretty old and I couldn't get it to actually run. So any recommendations are welcome that include powershell or batch
C:\Temp isn't a file; it's a folder. Try specifying an actual filename, like this:
#echo off
cd C:\Program Files\Wireshark
Wireshark.exe -i 4 -k -a duration:10 -w C:\Temp\foo.pcapng
pause

Running executable script from iCloud folder

I'm running an executable script from my iCloud folder. I get the following error
"ERROR: run_shell_command on /Users/username/Library/Mobile Documents/com~apple~CloudDocs... /bin/bash: /Users/username/Library/Mobile: No such file or directory"
which has to do with the space between "Mobile" and "Documents" in the iCloud path. How do you handle these cases. I'm sure there is an easy solution but I can't figure it out.
I've checked running the script in my local machine and it works. However, I would prefer to do this from the cloud to optimize space.
Thank you
Spaces are escaped with the \ character.
For example:
cd /Users/username/Library/Mobile\ Documents
You can also enclose the argument in quotes:
cd '/Users/username/Library/Mobile Documents'
This solves the space-in-path problem on iCloud Drive persistently:
# bash 5 will expand $HOME to your home dir path.
# Or, replace '$HOME' with /Users/<username>
cd ~
ln -s "$HOME/Library/Mobile\ Documents/com~apple~CloudDocs" icloud
cd ~/icloud
From ~/icloud I enjoy command line access. My bash autocomplete works. I can ignore the tildes and space. I use unix naming for the files and directories below ~/icloud, to make script access easier.
#cmartinezvil if you get tired of quoting paths every time, the symlink is handy. You can get the docs using info ln on MacOs.
Thanks go to #jl-peyret for this answer.
https://apple.stackexchange.com/questions/428604/how-to-make-icloud-drive-conveniently-available-to-the-shell/448685#448685

Download zip file from s3 using s3cmd issue

I have two issues I need help with on bash, linux and s3cmd.
First, I'm running into linux permission issue. I am trying to download zip files from a s3 bucket using s3cmd with following command in a bash script.sh:
/usr/bin/s3cmd get s3://<bucketname>/<folder>/zipfilename.tar.gz
I am seeing following error: permission denied.
If I try to run this command manually from command line on a linux machine, it works and downloads the file:
sudo /usr/bin/s3cmd get s3://<bucketname>/<folder>/zipfilename.tar.gz
I really don't want to use sudo in front of the command in the script. How do I get this command to work? Do I need to give chown permission to the script.sh which is actually sitting in a path i.e /foldername/script.sh or how do I get this get command to work?
Two: Once I get this command to work, How do I get it to download from s3 to the linux home dir: ~/ ? Do I have to specifically issue a command in the bash script: cd ~/ before the above download command?
I really appreciate any help and guidance.
First, determine what's failing and the reason, otherwise you won't find the answer.
You can specify the destination in order to avoid permission problems when the script is invoked using a directory that's not writeable by that process
/usr/bin/s3cmd get s3:////zipfilename.tar.gz /path/to/writeable/destination/zipfilename.tar.gz
Fist of all ask 1 question at a time.
For the first one you can simply change the permission with chown like :
chown “usertorunscript” filename
For the second :
If it is users home directory you can just specify it with
~user
as you said but I think writing the whole directory is safer so it will work for more users (if you need to)

Cygwin to use my windows username in path?

I'm writing a script where Cygwin needs to cp a file from my desktop.
cp /cygdrive/c/Users/<username>/Desktop/file /tmp/file
How can I make Cygwin get my or the username of the person who is currently running it?
I found it in a different answer,
The profile of the current user is $USERPROFILE.
Putting them together it becomes:
cp $USERPROFILE/Desktop/file /tmp/file

Prevent user from changing directory using rbash

I have created two new users in my FreeBSD unix environment and I want to limit their access to their respective home directories only.
After reading on the internet, I found a solution to modify /etc/passwd file and change their bash to rbash which will prevent them to run CD command.
so I run the following command to find where my rbash is ?
where rbash
and it gives the following output,
usr/local/bin/rbash
so I opened my /etc/passwd file and change the /bin/sh to /usr/local/bin/rbash.
After that I logged out and logged back in using the account for which I modified the settings but I could still run the CD command, or any other command for that matter.
Can anyone please tell me why it is not working ?
Can this be done using Samba or any other solution ?

Resources