Running Perl script accidentally locked a folder - linux

I have a Perl script which has the following code
#/usr/bin/perl
use strict; use warnings;
chmod -R 775,"path-to-current-folder";
Upon running this script, I am unable to access the current folder (and open this script of course) anymore. When seeing the folder in Konqueror, there is an additional lock in the folder icon. Can anyone tell me what happened and how can I undo this?
I have checked the permission of this folder, apparently it was changed to d---------. I have solved this problem by resetting the permission, yet it would be great if anyone could explain why this happened. Thanks.

I think you're confusing the 'chmod' shell command with the 'chmod' perl function. The latter takes a single list as a parameter the first element of which must be the numeric code experessed in octal. From perldoc -f chmod ;
chmod LIST
Changes the permissions of a list of files. The first element
of the list must be the numeric mode, which should probably be
an octal number, and which definitely should not be a string of
octal digits: 0644 is okay, but "0644" is not. Returns the
number of files successfully changed. See also "oct" if all
you have is a string.
$cnt = chmod 0755, "foo", "bar";
chmod 0755, #executables;
... etc ...
The former - that is, the shell - has a -R switch. See man chmod for details.

Related

softlink to binary always use home folder path (instead of current folder)

kdevelop provides this AppImage binary:
wget -O KDevelop.AppImage https://download.kde.org/stable/kdevelop/5.1.1/bin/linux/KDevelop-5.1.1-x86_64.AppImage
chmod +x KDevelop.AppImage
./KDevelop.AppImage
It works well. So I want to make a soft link called kd to that binary in /usr/bin, eg:
/usr/bin/sudo ln -s KDevelop-5.1.1-x86_64.AppImage kd
Now if I run kd file1, I'd expect that it would open a file name file1 in the current folder, but it always tries to open a file name file1 in my home folder - which is not where it should be.
Is there some way to fix this issue?
Some possible causes:
The application always assumes that you want to open files in your home directory, effectively or literally prepending $HOME to the path. This would be a bug in any *nix program, and should be reported.
The application behaves differently when $(basename "$0") is not KDevelop.AppImage (what #Scheff said).
You are actually running a different kd.
Possible workarounds/investigations:
Pass the full path to the file on the command line. If it tries to open /home/you//full/path/you/provided it is obviously buggy, and you have a test case. If it does not, then there might be some gotcha to what your $PWD actually is. Try checking its value before running.
Symlink with the same name, using sudo ln -s KDevelop-5.1.1-x86_64.AppImage /usr/bin, and try running that. If it behaves the same, you've at least proven that the symlink is not the problem.
Run type -a kd and verify that your /usr/bin/kd comes up first. If not there might be an alias or shell built-in which takes precedence.
That said, what is the actual error message?

shell script : appending directory path and filename

I want to copy a file from a directory using shell script
Suppose I save the directory and file name seperately as
dir=/home/user/directory/
file=file_1
to copy the file Im using this command in my script
cp $dir$file .
But I get this error
/bin/cp omitting directory '/home/user/directory'
I have tried all combination eg. omitted the trail backslah from variable dir, etc but nothings working. I cant understand what is wrong with this code. Pleas help
Maybe the command $dir$file is not getting unpacked in the shell (ie only the directory variable is getting unpacked, not the file variable)!!!!!
It looks like you are having problem with expansion in cp $dir$file . In order to prevent possible problems, it is better to protect your variable with braces and double quote the full path/file to make sure you don't get caught by spaces in either the filename or heaven forbid the user's dirname:
cp "${dir}${file}" .
This will prevent the possibility the second $ is missed. Also make sure you have read access to other users /home (if you are root or using sudo you should be fine)
If you see this, when you somehow assign an empty string to file somewhere. Search your script for file= and unset file.
You can also debug this by adding
echo ".${file}."
in the line before the cp command. I'm pretty sure it prints .., i.e. the variable is empty or doesn't exist.

Sudo will break importing my script

I have a shell script (my.sh) which imports another file (myfile.env). The name of myfile.env is passed as an argument to my.sh. It works well as root and I want the ability to run it to be given for other users via sudo.
myfile.env
some shell scripts........
my.sh
ENVFILE="$1"
if [ -e $ENVFILE ] ; then
. $ENVFILE
fi
I call the my.sh as
sudo /abc/def/my.sh myfile.sh
sudoers file goes as below
Cmnd_Alias MYSUDO = /abc/def/my.sh, /bin/bash
%mygroup ALL = MUSUDO
Unfortunately this results with an error in the 3rd line in the my.sh code above. It says
.: myfile.env: cannot open [No such file or directory]
its funny that i get this error after a check on file exist returned true.
sudo -s and -E options didn't help (don't know whether I used them correctly)
Can someone please help me to resolve this? I am using CeNT OS 6.5
use sudo /abc/def/my.sh /absolute/path/to/myfile.env
If myfile.sh is supposed to be in the same dir as my.sh, then my.sh can do this:
ENVFILE=$(dirname "$0")/"$1"
Finally I was able to get it done. It was setting in the sudoer file to get the directories (the folder in with those files were) into safe path list.
All I had to do was,
run visudo
and append the two dolders (where the files are) into
Defaults secure_path list
That will add those directories into the PATH when a user get elevated rights.
https://askubuntu.com/questions/128413/setting-the-path-so-it-applies-to-all-users-including-root-sudo

command not found in bash-3.2$

I tried running a script file using bash but it showed an error
bash-3.2$ example.sh : command not found
I also tried
ls -l example.sh
I found that it was not executable, so I used
sudo chmod 777 example.sh
I again tried running it but same error was coming. I double checked that I am in the same folder as the file using ls. But still I am not able to execute the script file.
I finally tried making a dummy script file and running it , and found the same error
I think there is some problem with BASH. Can some one help me with what is the problem?
I am working on redhat, bash was already installed in my system
Since I am newbie on linux any help would be appreciated
bash search for commands in your $PATH. Apparently the current directory, ., is not in your $PATH. (This is a good thing; having . in your $PATH is insecure.)
You'll need to specify a directory name. Just type:
./example.sh
Incidentally, doing:
sudo chmod 777 example.sh
is two kinds of overkill. First, you don't need to use sudo; use sudo only when you actually need to. Presumably your personal account owns the file, so you can just use chmod directly.
Second, 777 is way too permissive. It allows anyone on the system to read, execute, or modify example.sh. (If you're the only person on the system it may not matter much, but it's still a bad habit.) Typically you should use 755 for directories and for files that need to be executable, and 644 for files that don't need to be executable.
Or just use
chmod +x example.sh
to set execute permission (your umask will prevent that from setting the permissions too loosely).
. (the current directory) is probably not on your path. Try ./example.sh or bash example.sh. You could also add . to your PATH environment variable, but that's generally frowned upon.
Your bash PATH probably doesn't include ., try running it by typing:
./example.sh
When you type a command, your shell searches your path to try to find the command, if the current directory (e.g. .) isn't part of the path, the script that you are trying to run won't be found. You'd have to explicitly give it the path to where this command is. And since it's in your current directory, you can just add ./ in front of the command.
first confirm the bash path
to check the path of bash use:
which bash
if you get "/bin/bash"
then add
#!/bin/bash
...
...
or whatever is the path on first line of your bash script

Create directory by mkdir

I want to create a folder named by the user name in /tmp/vnc/, I can create that folder in command line with perl -e 'mkdir("$ENV{USER}")', but for the following code cannot work.
chdir ("/tmp/vnc") or die -1;
mkdir ("$ENV{USER}", 0777) or die -1;
If I use mkdir -p /tmp/vnc/$ENV{USER} in command line to make folder, nothing happens and no error reports.
It works for me.
Maybe the /tmp/vnc directory does not exist, and the chdir fails.
Or maybe the $USER environment variable is not defined, because you are running it from a init.d script, for example...
Or maybe you do not have write permissions in the /tmp/vnc directory. Have you tried executing mkdir /tmp/vnc/$USER from the shell?
Impossible to know more without details.
Please check special variable $! for text error message
Please check that variable $ENV{USER} doesn't contain extra quotes. I had similar problem in Windows OS for Activer Perl. My problem was in extra quotes
Perhaps something is resetting your environment when you are running the script? Can you print the contents of $ENV{USER} and make sure it contains what you think it should?
if $ENV{USR} is an absolute path (with the leading slash), then the chdir is useless since you are not using a relative path

Resources