This question already has answers here:
Are shell scripts sensitive to encoding and line endings?
(14 answers)
Read a file line by line assigning the value to a variable [duplicate]
(10 answers)
Closed 2 months ago.
New to coding
I have a very simple loop that Im using to rename files, but it stops after the first iteration.
Im running the loop:
for x in `cat list_genomes.txt`
do
mv -v $x"_busco/run_hypocreales_odb10" "/busco_runs/run_"$x
done
and the error I get is:
‘FV25228_busco/run_hypocreales_odb10’ -> ‘busco_runs/run_FV25228’
mv: cannot stat ‘\r_busco/run_hypocreales_odb10’: No such file or directory
mv: cannot stat ‘FV32968\r_busco/run_hypocreales_odb10’: No such file or directory
mv: cannot stat ‘FV34765\r_busco/run_hypocreales_odb10’: No such file or directory
mv: cannot stat ‘FV-NC-01\r_busco/run_hypocreales_odb10’: No such file or directory
so the first line works, but then it cannot read the rest.
list_genomes.txt looks like:
FV25228
FV32968
FV34765
FV-NC-01
Fc25332
Your input file has originated on a system that is not identical to the one where you are running the script.
The "\r" on the 2nd-5th line indicate that the file came from a Windows-based system. The person who created the file should have chosen a format that is compatible with system on which the data was going to be processed, which in this case appears to be Unix/Linux based.
If the creator of your input file doesn't change the way they do that, you need to massage the file before feeding it to your program logic, using the command dos2unix.
The following would likely provide you with the necessary fix if all else is correct:
#!/bin/bash
dos2unix -n list_genomes.txt list_genomes.fix
while read line
do
mv -v "${line}_busco/run_hypocreales_odb10" "/busco_runs/run_${line}"
done < list_genomes.fix
Related
This question already has an answer here:
Bash how do you capture stderr to a variable? [duplicate]
(1 answer)
Closed 1 year ago.
I have been trying to write a program that will take the volume adjustment stat in Sox, and store it in a variable.
To do this you have to do
sox *your-audio-file* -n stat,
and the final line will show the stat that I want.
However, when I try to store the whole output of that command in the variable INITSTAT, it remains blank, and the line of code that should be storing the output in the variable is just printing the output to the terminal. This is what I have:
INITSTAT=`sox $audioFilePath -n stat`
echo $INITSTAT
Where "$audioFilePath" is the path to the audio file I am trying to get the information about.
If anybody knows what is wrong, any help would be appreciated.
I suggest to redirect stderr to stdout:
INITSTAT=$(sox "$audioFilePath" -n stat 2>&1)
This question already has answers here:
Are shell scripts sensitive to encoding and line endings?
(14 answers)
Closed 1 year ago.
I am trying to write a bash script that takes 2 inputs: a list of IDs and 2) a directory. The idea is that the script will move a subset of files corresponding to the list of IDs into a a new folder.
#! /usr/bin/bash
LIST_FILE=$(cat ${1}) # List of file IDs (example entry: 1.2345)
PATH=${2} # directory name (i.e group_1)
for i in ${LIST_FILE}
do
/usr/bin/mv /tmp/project/project_data/data_all/${i}.annotated.gz /tmp/project/project_data/${PATH}/
done
The script manages to loop ok, however I get the following error for each iteration:
/usr/bin/mv: cannot stat '/tmp/project/project_data/data_all/1.2345'$'\r''.annotated.gz': No such file or directory
It looks like the file name hasn't concatenated properly and I'm not sure why. I've tried researching the problem but I'm also quite new to bash and finding it hard to grasp the concept of the stat error. I appreciate any advice and possible solutions.
Thanks everyone.
I think that the file whose name you pass as the first argument to your script is in dos format instead of unix so you are getting extra \r characters in your file names.
You could change your third line to:
LIST_FILE=$(cat ${1}|tr -d '\r')
Bobby
This question already has an answer here:
Permission denied on cat via shell script [duplicate]
(1 answer)
Closed 3 years ago.
I'm trying to take the filename of each file in a directory and 'rename' it to create a respective output file when running through a program. When running the script I get the error Permission denied, for the line that is meant to be doing the renaming.
outputname=basename $file | sed -e "s/_Aligned.sortedByCoord.out.bam/_.gtf/"
For example one file is named 92_Aligned.sortedByCoord.out.bam, I want the output to be 92_.gtf. Another file is called 10.5_rep1_Aligned.sortedByCoord.out.bam, and the output should be called 10.5_rep1_.gtf.
Am I doing it wrong? Not sure if sed is the right way as I'm technically not renaming the file, but creating another file from that name and changing it?
You want to force evaluation on basename-sed pipe. ( with '$(' ) and handle the quoting)
outputname=$(basename $file | sed -e "s/_Aligned.sortedByCoord.out.bam/_.gtf/")
Or using Bash built-in only, which will be much FASTER.
file_base=${file##*/}
outputname=${file_base/_Aligned.sortedByCoord.out.bam/_.gtf}
This question already has an answer here:
Why would a correct shell script give a wrapped/truncated/corrupted error message? [duplicate]
(1 answer)
Closed 7 years ago.
I am trying to run this simple bash script:
file_name=deploy
echo "Init File NAme $file_name"
current_time=$(date "+%Y.%m.%d-%H.%M.%S")
echo "Current Time : $current_time"
new_fileName="${file_name}${current_time}.zip"
echo "New FileName: $new_fileName"
#echo $new_fileName ./app/code/community ./app/code/local ./app/design/frontend/indigo ./app/design/frontend/default
#zip $new_fileName ./app/code/community ./app/code/local ./app/design/frontend/indigo ./app/design/frontend/default
But for some reason I receive:
Init File NAme deploy
Current Time : 2015.10.01-16.04.02
./ManualPack.sh: line 5: $'\r': command not found
.zip5.10.01-16.04.02
I know its very very simple to any beginner in bash, but I tried for a decent amount of time, with many Stack Overflow threads to make it work, but the output remains the same.
You seem to have Windows styled line terminators in your file (\r\n). Converting the file with dos2unix should help.
This question already has an answer here:
Why would a correct shell script give a wrapped/truncated/corrupted error message? [duplicate]
(1 answer)
Closed 7 years ago.
I have 100 ".txt" files. Each file contains the data such as
File name Data
1.txt BAP1
2.txt UCHL1
3.txt ABC1234
Now I want to scan content of these files and write to txt file with condition that it contains my input string such as "BAP1". I used below coded but the output files have mistaken such as '1.txt .' I have no idea why the extension file has more dot in last file. Could you help me solve it? I am working in cygwin
#!/bin/sh
grep -w 'BAP1' *.txt>"1.txt"
grep -w 'UCHL1' *.txt>"2.txt"
Run dos2unix on your script, or otherwise tell your editor to save it as a UNIX text file.
Otherwise, your filenames will have carriage returns (aka $'\r') on the end of their names.