Ubuntu Shell Script Output Directory - bash

I don't use bash much, but lately I've had to use it and I have been continuously running into an issue with command-line arguments, specifically output directories. Some example code below:
Running:
log2timeline -f win7 -z EST5EDT -r -p -o csv -w /home/sansforensics/Desktop/PATH/file /mnt/hgfs/DRIVE/INPUT
on the command line works exactly as expected, the -w flag signifies the output path/file and that is exactly where it ends up, on the user's desktop in the PATH folder.
But when I run it from a .sh file sitting on the Desktop of the same user, i.e.
sudo bash test.sh
and all the file contains is the same exact command, the output file does not show up in the expected location. What am I missing here??

Related

Perl installed via conda snakemake shell error: Can´t open perl script .. no such file or directory

I´m currently writing a snakemake pipeline, for which I want to include a perl script.
The script is not written by me, but from a github page. I never worked with perl before.
I installed perl (5.32.1) via conda. I have installed miniconda and am working on my universities unix server.
The code for my perl script rule looks like this:
rule r1_filter5end:
input:
config["arima_mapping"] + "unprocessed_bam/{sample}_R1.sam"
output:
config["arima_mapping"] + "filtered_bam/{sample}_R1.bam"
params:
conda:
"../envs/arima_mapping.yaml"
log:
config["logs"] + "arima_mapping/r1_filter5end/{sample}_R1.log"
threads:
12
shell:
"samtools view --threads {threads} -h {input} -b | perl ../scripts/filter_five_end.pl | samtools -b -o {output} 2> log"
When I run this I receive the following error:
Can't open perl script "../scripts/filter_five_end.pl": no such file
or directory found
From what I learned while researching is that the 1. line of a perl script sets the path to my perl executable. The script I downloaded had the following path:
#!/usr/bin/perl
And since I use perl installed via conda this is probably wrong. So I set the path to:
#!/home/mi/my_user/miniconda3/bin/perl
However this did still not work, regardless of if I call
perl ../scripts/filter_five_end.pl
or
../scripts/filter_five_end.pl
Maybe it´s just not possible to run perl scripts via snakemake?
Anyone who had encountered this specific similar case?^^
The problem is not with the shebang. The interpreter path in the shebang doesn't matter because you're calling it with perl ../path directly. The shell that this command is executed in will resolve the path to the perl program (which is very likely the conda one) and then run the script, only taking flags (like -T or -w) from the shebang inside the script.
The error message means it cannot find the actual script file. I suspect when you run that shell command, it's in the wrong directory. Try a fully qualified path.
As stated by OP in their comment:
I forgot that snakemake always looks files up from the Snakemake file not the directory the rules are saved in.
Not quite an answer but perhaps relevant:
I forgot that snakemake always looks files up from the Snakemake file not the directory the rules are saved in.
This is not entirely correct, I think. The reference point is the directory set by -d/--directory which by default is where you execute snakemake:
--directory DIR, -d DIR
Specify working directory (relative paths in the snakefile will use this as their origin). (default: None)

Cygwin execution of .sh file can't find grep command?

So I was trying to create little .sh script for my work and run into one little problem.
My cygwin terminal (x64) runs just fine and I'm using it often enough to do manual greps.
In the begging I had some issues with this command but now it works fine in cygwin terminal.
Once I wrote my little script and tried to run it only output I'm getting is "line 6: grep: command not found"
My execution method is:
Open cygwin terminal
cd to script location
type in ./script.sh
enter :)
Anyone knows how to fix that? I already added cygwin bin folder to my system path (Win 10 btw) but that didn't helped. Looked around for a while but haven't found anything useful, mostly issues with grep itself.
my script for reference:
mkdir -p output
PATH=$PWD"/output"
while IFS=";" read -r component location global
do
cd $location
grep -iRl $global --exclude-dir={wrongdir1,wrongdir2} > $PATH"/"$component".txt"
done < input.csv
you're overwriting you Cygwin system path: PATH=$PWD"/output" - instead of PATH use a diff var name.

wildcard search in sudo argument not working

I am not able to use wildcard in command arguments when not using -i option. What can be the reason?
Below result with -i option:
Command - sudo -i -u \#800 ls -l /LOG/filename.*
Result - filename.dat
Below result without -i option:
Command - sudo -u \#800 ls -l /LOG/filename.*
Result - filename.* not found
Your results do not match your command so I'm not really trusting the results that you wrote into the question. In particular /LOG/ is left off of your results.
e.g.
sudo -i ls -ld /var/folx*
ls: /var/folx*: No such file or directory
I don't know which sudo you are using because AIX does not come with a sudo command. But I'm using the man page from my Mac.
-i [command]
The -i (simulate initial login) option runs the shell
specified in the passwd(5) entry of the target user as a
login shell. This means that login-specific resource files
such as .profile or .login will be read by the shell. If a
command is specified, it is passed to the shell for
execution. Otherwise, an interactive shell is executed.
sudo attempts to change to that user's home directory
before running the shell. It also initializes the
environment, leaving DISPLAY and TERM unchanged, setting
HOME, MAIL, SHELL, USER, LOGNAME, and PATH, as well as the
contents of /etc/environment on Linux and AIX systems. All
other environment variables are removed.
Note the phrase " to change to that user's home directory". It appears that it does the cd to home even when a command is given.
sudo pwd
/private/tmp
sudo -i pwd
/private/var/root
Here are some methods to try and debug this type of situation yourself. First is to replace your command with just env and capture the output in two separate files and then compare them. See if any differences might be the root of the issue. Second, pwd as I did and you discover that your current working directory is changing while in the sudo context. The third item which doesn't apply in this case but does in other cases is to do echo * as the command. In this case, it would have given you a clue but probably still might have been really confusing.
The other part is notice that -i is sucking up .profile and .login. Many users goof up their .profile and .login files by assuming various things. So, the other item that you might need to do sometimes is put set -x at the top of your .profile to see what it is doing. In this case, that was not needed.

Running a bash shell script on Windows from within in Notepad++ using NppExec (and Cygwin)

I'm trying to reconstruct how to execute a bash shell .sh file on my Windows machine from within Notepad++ using NPPExec. (I've done this successfully before, but my HDD crashed and I don't recall how I did it previously.)
When it was working before, I would run the NPPExec script that called a .sh file, and it showed me the console output of the .sh file processing in Notepad++ as if it were processing in cygwin.
This is the example .sh file that I'm trying to get to work:
message="Testing"
echo $message
This file is located in the root of C:.
Failed Attempts:
None of the following three methods work:
Execute: C:\nppexec.sh
Response:
CreateProcess() failed with error code 193:
%1 is not a valid Win32 application.
Execute: npp_exec C:\nppexec.sh
Response:
message="Testing"
CreateProcess() failed with error code 2:
The system cannot find the file specified.
$message
Adding #! /bin/bash to the .sh file as the first line just causes an additional error when npp_exec is run:
NPP_EXEC: C:\nppexec.sh
#! /bin/bash
CreateProcess() failed with error code 2:
The system cannot find the file specified.
The solution was to call bash directly:
C:\cygwin\bin\bash --login -c "command.sh"
I have the same error while trying to execute a batch file on windows.
I resolved the problem by executing at first command cmd in console of notepad++, then E:\test.bat
I also have a mksnt installed on my window pc.
by starting at first the bash in console of notepad++, the test shell work well now
bash
C:\nppexec.sh
With a single keystroke I wanted to execute the shell script of the active Tab using Cygwin within notepad.
After few hours looking online and experimenting, I finally came up with
install NppExec plugin
Hit F6
paste the following code:
//save the file
NPP_SAVE
//redirect console output to $(OUTPUT) & silent mode
npe_console v+ --
//convert winpath to cygpath
D:\cygwin64\bin\bash -lc "cygpath \"$(FULL_CURRENT_PATH)\"
//execute the file
D:\cygwin64\bin\bash -lc "$(OUTPUT)"
Hope that save some time to some people
Use this Run command
C:\cygwin64\bin\bash.exe -l -c "cd \"$0\" ; echo $#; \"./$1\"; exec bash;" "$(CURRENT_DIRECTORY)" "$(FILE_NAME)"
You can save this command for later use from Run dialog box.
For git bash, change the path like this:
C:\Progra~1\Git\bin\bash -l -c "cd \"$0\" ; echo $#; \"./$1\"; exec bash;" "$(CURRENT_DIRECTORY)" "$(FILE_NAME)"

How to create and run a bash file?

How to create a bash file?
I found following bash script.
#!/bin/bash
for f in *.wav; do
echo "Processing $f file..."
afconvert -f caff -d LEI16#44100 -c 1 "$f" "${f/wav/caf}"
done
I have Mac and Windows.
But I don't know where I have to put this and how I can run this script?
Just save the text into a file called my_bash_script a file, make it executable and run it from the command line like this:
chmod u+x my_bash_script
./my_bash_script
Judging by the file it will need to be in a directory containing *.wav files.
As you have two different OS setup, I will split my answer in two parts.
First: Windows
Windows does not have a Bash interpreter, nor the afconvert program the code above is trying tu run. Your best bet will be to use Cygwin to install a Unix console on your Windows. Also I don't know, where you could get afconvert from.
OSX
OSX does have a console and the afconvert software (at least my OSX does). You can simply drop the file in a folder and give it a name ending in .sh. Then you should be able to run it.
Just paste the text into a plaint text file and then mark it as executable:
chmod +x yourScript
To run it:
./yourScript
Place the script anywhere you have your .wav files. When fallow instructions given here:
http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_02_01.html#sect_02_01_03

Resources