Give permission 777 for UNIX files using shell script - shell

I want to give 777 permission for the files in UNIX and change that file as DOS file.
I want to achieve this in shellscript file. I will pass the partial file name from command prompt. Example: if the file name is employeesalary, employeejob then if i pass employee in the command prompt then all the file which starts with employee will be given access to 777 and also it needs to be changed as DOS file.
filename={$1}
chmod 777 $filename*
u2d -i $filename*
When i run the above code i am getting the below error.
chmod: WARNING: can't access employee*
can't open employee*: No such file or directory in some location it specified
But when i run these commands alone in command prompt its working fine
chmod 777 employee*
u2d -i employee*

There's no need for a separate variable here. Just do
chmod 777 "$1"* && u2d -i "$1"*
If you prefer it as three lines:
filename="$1"
chmod 777 "$filename"* || exit $?
u2d -i "$filename"*
That said, 777 (world-writable, world-executable) is probably not a good idea. Would 755 (rwxr-xr-x) or even 644 (rw-r--r--) work for you? If so, that would be better.

Related

Commands without sudo in bash do not work

I am running a bash script and these commands in the script will not work without sudo in front of them. The script.sh is located in a folder such as /jobs/script.sh
Example of commands I am trying to run in the script.sh -
mv /var/app/myapp /var/app/myapp.old
rm file.tar.gz
tar -xzf /home/ubuntu/file.tar.gz -C /var/app/
All the above work if I add sudo in front of them.
I am trying to figure out what permissions are required for them to work without adding sudo in the script.
I have tried giving the script.sh rwx permissions and changing owner to root.
I'm learning permissions in linux, so I'm new to this. Basically what permission should the script.sh have so that I dont have to use sudo in the bash file? Any insight would greatly help.
When you run sudo <some command>, then <some command> is run by the root user (Super user do). The reason you might need to run any command using sudo is because the permissions on the files that command reads/writes/executes are such that only the "Super user" (root) has that permission.
When executing the command mv fileA fileB, the executing user would need:
Write permission to fileB if fileB already existed
Write permission to the directory containing fileB
From what you said it’s most likely you want read and write permissions you can achieve this with chmod
Chmod +[permission] filename
(+ is used to add permission you can also use - instead to remove it)
Where permissions can be:
r —> read
w—> write
x —>excecute
... and more
FOR EXAMPLE: it seems you write permissions for the first file so :
chmod +w /var/app/myapp
Will fix problem

Chmod doesn't change permissions

I have a file named replace.txt with 777 permissions. when running the command:
chmod 755 replace.txt
the permissions don't change accordingly. I checked with the
ls -l
command and it still shows the same permissions as before.
I've tried to run the command as:
sudo chmod 755 replace.txt
and it still won't work. Furthermore, I checked if the command was successful by using
echo $?
and it returns 0, meaning it was successul. Yet, no permissions were changed.
Any idea why is this happening?

How to suppress runtime questions while changing file permission in unix

We calling a PLSQL program unit from shell script, and the PLSQL program unit writes a file in database file system mount location with 644 permission. And then finally the shell script attempts to change the file permission mode to 764 using below statement.
chmod 764 $file
During run time, script is requesting for user input to change the file permission.
override mode 644 on /path/to/file/filename?
How to suppress this or is there any way to provide 'Y' in the chmod command itself ?
To suppress chmod messages you can add -f flag as:
chmod -f 764 $file
-f, --silent, --quiet suppress most error messages

Chown directory via SSH on server using NPM script

I am trying to chown a directory via an NPM script. The script looks like the following:
chown -R 755 www-data root#XXX.XXX.XXX.XX:/var/www/test.com
But the message I get back is: chown: www-data: No such file or directory even though this exists. Any ideas much appreciated.
chown operates locally, not on remote servers. In your example, chown is attempting to operate on ./www-data and ./root#XXX.XXX.XXX.XX:/var/www/test.com, which don't exist in the directory of wherever you were when you executed the command.
You will need to execute chown as a command through ssh:
ssh root#XXX.XXX.XXX.XX chmod -R 755 /var/www/test.com/
Fixed this with following script.
ssh root#XXX.XXX.XXX.XX chmod -R 755 /var/www/test.com/
(I needed to login to the server first).

cannot open output file a.out: Permission denied, on a simple compilation

I wrote some code in vim and whenever I try to run the code it shows this:
coolmego#coolmego-PC:~/coolmego/cprograms$ gcc dfs8puzz.c
/usr/bin/ld: cannot open output file a.out: Permission denied
collect2: ld returned 1 exit status
coolmego#coolmego-PC:~/coolmego/cprograms$ ./a.out
bash: ./a.out: No such file or directory
What should I do?
Move to a directory where you are allowed to write.
This is because if you only have write permissions, but you are not the owner the directory.
Check your user name:
whoami
Make yourself the owner of the directory and its contents:
sudo chown -R "$USER:" /path/to/the/directory
Set read/write/execute permission
chmod -R 700 /path/to/the/directory
refer https://askubuntu.com/questions/466605/cannot-open-output-file-permission-denied
When you run sudo, you are actually running the commands as root user. Possibly you ended up messing up the permissions so that root owns the files. Thus when you run sudo, it just works (root can write in those directories). You need coolmego to own those files. For example:
sudo chown coolmego /home/coolmego/coolmego/cprograms/
chmod 700 /home/coolmego/coolmego/cprograms/
Remove option user in /etc/fstab. Anything with user in the fstab is automatically mounted noexec unless exec is explicitly given in the fstab.
Try giving read write permission to the directory in which you are targeting to get the output. In case you are using a personal system you can do "sudo chmod 777 "
I was having the same problem, after 1 hour i found out it was my Antivirus, i shut that down and everything worked fine.
try chmod -R 777 ~/coolmego/cprograms

Resources