Create directory by mkdir - linux

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

Related

linux source a symbolic link

I have a bash script which i want to call from any directory, but i don't want to add the directory it is in to PATH as it is filled with lots of other scripts which will just clutter.
The script in question manipulates environment variables, so i have to source it.
I tried creating an alias
alias aliastoscript="/path/to/script"
source aliastoscript #This does not work says no such file
I also can't copy the script itself to a different location as it depends on the directory structure and other scripts in the directory.
So i tried a symlink to a location already in path:
ln -s /path/to/script /directory/already/in/path/myscript
But this does not work either:
source myscript #says no such file exists
Can anyone suggest how i achieve this? And why does the symlink approach not work?
If it makes any difference, i am using a zsh shell on ubuntu 14.04
EDIT:
The answer given below works, but i also wanted to know why the symlink approach was not working.
Here is the sequence of commands
ln -s /path/to/script /directory/already/in/path/myscript
#Now there is a symlink called myscript in a directory which is in PATH
source myscript arg1 #This throws an error saying no such file myscript,
#but it is not supposed to happen because myscript resides in a directory which is in PATH
EDIT 2:
I just figured what i was doing wrong, the symlink i created, i had used relative paths, totally stupid of me, using absolute paths it worked like a charm.
Try replacing:
alias aliastoscript="/path/to/script"
with:
export aliastoscript="/path/to/script"
You have a $ missing in front of the variable name.
source $aliastoscript
You do not need soft link for the source. The complete file name should work. Better is
source /path/to/script

Running Perl script accidentally locked a folder

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.

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.

Add a bash script to path

I want to add a small script to the linux PATH so I don't have to actually run it where it's physically placed on disk.
The script is quite simple is about giving apt-get access through a proxy I made it like this:
#!/bin/bash
array=( $# )
len=${#array[#]}
_args=${array[#]:1:$len}
sudo http_proxy="http://user:password#server:port" apt-get $_args
Then I saved this as apt-proxy.sh, set it to +x (chmod) and everything is working fine when I am in the directory where this file is placed.
My question is : how to add this apt-proxy to PATH so I can actually call it as if it where the real apt-get ? [from anywhere]
Looking for command line only solutions, if you know how to do by GUI its nice, but not what I am looking for.
Try this:
Save the script as apt-proxy (without the .sh extension) in some directory, like ~/bin.
Add ~/bin to your PATH, typing export PATH=$PATH:~/bin
If you need it permanently, add that last line in your ~/.bashrc. If you're using zsh, then add it to ~/.zshrc instead.
Then you can just run apt-proxy with your arguments and it will run anywhere.
Note that if you export the PATH variable in a specific window it won't update in other bash instances.
You want to define that directory to the path variable, not the actual binary e.g.
PATH=$MYDIR:$PATH
where MYDIR is defined as the directory containing your binary e.g.
PATH=/Users/username/bin:$PATH
You should put this in your startup script e.g. .bashrc such that it runs each time a shell process is invoked.
Note that order is important, and the PATH is evaluated such that if a script matching your name is found in an earlier entry in the path variable, then that's the one you'll execute. So you could name your script as apt-get and put it earlier in the path. I wouldn't do that since it's confusing. You may want to investigate shell aliases instead.
I note also that you say it works fine from your current directory. If by that you mean you have the current directory in your path (.) then that's a potential security risk. Someone could put some trojan variant of a common utility (e.g. ls) in a directory, then get you to cd to that directory and run it inadvertently.
As a final step, after following the solution form proposed by #jlhonora (https://stackoverflow.com/a/20054809/6311511), change the permissions of the files in the folder "~/bin". You can use this:
chmod -R 755 ~/bin
make an alias to the executable into the ~/.bash_profile file and then use it from anywhere or you can source the directory containing the executables you need run from anywhere and that will do the trick for you.
adding to #jlhonora
your changes in ~./bashrc or ~./zshrc won't reflect until you do
source ~./zshrc or source ./bashrc , or restart your pc

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

Resources