Passing Path as parameters in unix script - unix

My script has two functions running.
1. checking of file
(it has to check file in path /IIS/Data/arrival)
2. archiving of file
(it has to archive files in path /IIS/Data/archive)
how to pass this paths as parameter?

Not sure you are using shell script or not. In shell, we can pass path as parameter, shell treats it like string.
$ cat ./test.sh
#!/bin/sh
function check_file {
file=$1
ls $file
}
check_file $1
the output:
$ ./test.sh Downloads/bleachbit-1.10-1.1.fc20.noarch.rpm
Downloads/bleachbit-1.10-1.1.fc20.noarch.rpm

Related

Check if file exists or not in KORN Shell

I want to check if file exists are not in a Korn shell, but not able to get a proper documentation for that. I have the following code that checks if file exists and is of size zero. If the file size is more than zero it returns false.
if [[ ! -s ${abs_file_name} ]]
I need a list of possible options (like -s, -e, -x, etc like in the above example) with description to check if file exists in KORN shell, NOT BASH shell.
You can check if a node exists with
if [ -e "${abs_file_name}" ]
You can check if a node is a file.
if [ -f "${abs_file_name}" ]
This does something dumb if abs_file_name resolves to a symlink to a file.
Also: -d for directory, -r for you can read it, -x for executable.

Specify which shell to use in R

I have to run a shell script inside R. I've considered using R's system function.
However, my script involves source activate and other commands that are not available in /bin/sh shell. Is there a way I can use /bin/bash instead?
Thanks!
Invoke /bin/bash, and pass the commands via -c option in one of the following ways:
system(paste("/bin/bash -c", shQuote("Bash commands")))
system2("/bin/bash", args = c("-c", shQuote("Bash commands")))
If you only want to run a Bash file, supply it with a shebang, e.g.:
#!/bin/bash -
builtin printf %q "/tmp/a b c"
and call it by passing script's path to the system function:
system("/path/to/script.sh")
It is implied that the current user/group has sufficient permissions to execute the script.
Rationale
Previously I suggested to set the SHELL environment variable. But it probably won't work, since the implementation of the system function in R calls the C function with the same name (see src/main/sysutils.c):
int R_system(const char *command)
{
/*... */
res = system(command);
And
The system() library function uses fork(2) to create a child process that executes the shell command specified in command using execl(3) as follows:
execl("/bin/sh", "sh", "-c", command, (char *) 0);
(see man 3 system)
Thus, you should invoke /bin/bash, and pass the script body via the -c option.
Testing
Let's list the top-level directories in /tmp using the Bash-specific mapfile:
test.R
script <- '
mapfile -t dir < <(find /tmp -mindepth 1 -maxdepth 1 -type d)
for d in "${dir[#]}"
do
builtin printf "%s\n" "$d"
done > /tmp/out'
system2("/bin/bash", args = c("-c", shQuote(script)))
test.sh
Rscript test.R && cat /tmp/out
Sample Output
/tmp/RtmpjJpuzr
/tmp/fish.ruslan
...
Original Answer
Try to set the SHELL environment variable:
Sys.setenv(SHELL = "/bin/bash")
system("command")
Then the commands passed to system or system2 functions should be invoked using the specified shell.

Using "find" in csh script file

I run a .csh file in UNIX that contains the following script
#!/bin/tcsh -f
set path = "$1"
find "$path" -name myfolder
And get the following message
find: Command not found.
What am I missing?
Thanks
The $path variable is special - it tells the shell where to find tools like find. :-) Use a different variable name.
From your interactive shell, you can see what $path normally looks like by echoing it. The following is my path on my FreeBSD server:
ghoti% echo $path
/usr/local/sbin /usr/local/bin /usr/sbin /usr/bin /sbin /bin /home/ghoti/bin /usr/X11R6/bin /usr/games
If this list is replaced with something else, for example the contents of $1, then tcsh doesn't know to look in /usr/bin to find find:
ghoti% which find
/usr/bin/find
ghoti% set path = "hello world"
ghoti% which find
find: Command not found.
ghoti%

How to always have the same current directory in VIm and in Terminal?

I would like to my terminal current directory follows my VIM one.
Example:
In TERMINAL:
> pwd
=> /Users/rege
> vim
Then in VIM
:cd /Users/rege/project
<Ctrl-z>(for suspend)
In terminal
> pwd
=> /Users/rege/project
I`m using MacOS, zsh, tmux.
I need this because when Im trying to use tags in VIM, tags are check in project from my terminal directory not vim one.
So I need to change terminal current directory always when I change VIM current directory.
What kind of command do you issue in your shell after you suspend Vim? Isn't Vim's :!command enough?
With set autochdir, Vim's current directory follows you as you jump from file to file. With this setting, a simple :!ctags -R . will always create a tags file in the directory of the current file.
Another useful setting is set tags=./tags,tags;$HOME which tells Vim to look for a tags file in the directory of the current file, then in the "current directory" and up and up until it reaches your ~/. You might modify the endpoint to suit your needs. This allows you to use a tags at the root of your project while editing any file belonging to the project.
So, basically, you can go a long way without leaving Vim at all.
If you really need to go back to the shell to issue your commands, :shell (or :sh) launchs a new shell with Vim's current directory. When you are done, you only have to $ exit to go back to Vim:
$ pwd
/home/romainl
$ vim
:cd Projects
:sh
$ pwd
/home/romainl/Projects
$ exit
In bash or zsh and on Unix you can do this: current working directory of the process is represented in /proc/{PID}/cwd as a symlink to a real directory. Speaking about zsh the following code will do the job:
function precmd()
{
emulate -L zsh
(( $#jobstates == 1 )) || return
local -i PID=${${${(s.:.)${(v)jobstates[1]}}[3]}%\=*}
cd $(readlink /proc/$PID/cwd)
}
. Note: with this code you won’t be able to pernamently switch directories in terminal anymore, only in vim or for duration of one command (using cd other-dir && some command).
Note 2: I have no idea how to express this in bash. The straightforward way is to get PIDs of all children of the shell (using ps --ppid $$ -o CMD), filter out the ps process (it will be shown as a child as well), check that there is only one other child and use its PID like in the last line above. But I am pretty sure there is a better way using some shell builtins like I did with zsh’s $jobstates associative array. I also don’t remember what is the analogue of precmd in bash.
Another idea would be making vim save its current directory into some file when you do <C-z> and make shell read this in precmd:
" In .vimrc:
function s:CtrlZ()
call writefile([fnamemodify('.', ':p')], $CWDFILE, 'b')
return "\<C-z>"
endfunction
nnoremap <expr> <C-z> <SID>CtrlZ()
# In .zshrc
function vim()
{
local -x CWDFILE=~/.workdirs/$$
test -d $CWDFILE:h || mkdir $CWDFILE:h
vim $#
}
function precmd()
{
local CWDFILE=~/.workdirs/$$
test -e $CWDFILE && cd "$(cat $CWDFILE)"
}
. It should be easier to port above code to bash.
you can open a new terminal like this
:!xterm -e bash -c "cd %:p:h;bash" &
actually I write this in my .vimrc
nmap <F3> :!xterm -e bash -c "cd %:p:h;bash" &<CR> | :redraw!
For bash users coming by:
Vim: Save pwd at <c-z> (with map and getpwd()).
Bash: Before prompt command, goto directory indicated by vim with PROMPT_COMMAND.
.bashrc
PROMPT_COMMAND='read -r line 2>/dev/null </tmp/cd_vim'\
'&& > /tmp/cd_vim && cd ${line##\r};'$PROMPT_COMMAND
vimrc
function! s:CtrlZ() call writefile([getcwd(),''], '/tmp/cd_vim', 'b')
return "\<C-z>"
endfunction
nnoremap <expr> <C-z> <SID>CtrlZ()
This is ZyX answer edited for bash https://stackoverflow.com/a/12241861/2544873

Run cd in script and stay in that directory - 'source' command not helping

Tried using the answer found here:
How to run 'cd' in shell script and stay there after script finishes?
When I add the 'source' command, the directory is still unchanged after script runs, regardless of whether I execute 'source ' or call the script using an alias coded in cshrc.
Any help is much appreciated!
As you can see below, make sure your call to cd is not executing within a subshell. If it is, this won't work, source or not.
Script with cd in subshell
#!/bin/bash
( cd /etc ) # thie exec's in a subshell
Output
$ pwd
/home/siegex
$ source ./cdafterend.sh && pwd
/home/siegex
Script with cd not in subshell
#!/bin/bash
cd /etc # no subshell here
Output
$ pwd
/home/siegex
$ source ./cdafterend.sh && pwd
/etc
It was necessary to remove "/bin/" from the cd command within this script, in order for the command to work as intended. Removing this removes the subshell issue for this script. Also, coding "$1" in the ls command was invalid in this context.

Resources