I'm trying to build a script that ask for user to type the name of the file, and once written, it simply shows what is inside this file, entirely.
So for instance,
Let's say I have a directory located in, /home/evaluation/, which contains severals files :
In /home/evaluation/file01,
Lorem ipsum dolor sit amet.
In /home/evaluation/file02,
Lorem ipsum sit amet.
In /home/evaluation/file03,
Lorem ipsum dolor
I'm looking forward to build a script that will ask me to write, the file name, I want to read, and once this file written, it will show all its content.
So if I type : file01, it will show me :
Lorem ipsum dolor sit amet.
Else, if the file doesn't exist in the directory, then it shall be written : "no file found".
Try this and see if you get what you are looking for
#!/bin/bash
echo Enter file name # ask for the file name
read fileName # get the a file name from the user and store it in fileName
if [ ! -f $fileName ] ; then # check to see if the file exists
echo "File not found" # print a message if it does not
exit # all done so exit
fi
# cat the file if we are still here , ie the file exists
cat $fileName
if [] in bash defines a test so
and -f file_name checks to see if the file exists and is a regular file
so [ ! -f $fileName ] will be true if the file does not exist, so then the message will be printed, otherwise the contents will be printed
Related
for name in $FASTQ_DIR/*/R1/*.fastq.gz
do
echo "$name"
done
/lustre/scratch119/realdata/mdt1/team113/projects/im13_basespace_runs/FASTQ/220800636_walk_up_431_miseq/R1/MISEQ_431_Plasmid_library_sample_01_S1_R1_001.fastq.gz
/lustre/scratch119/realdata/mdt1/team113/projects/im13_basespace_runs/FASTQ/220800636_walk_up_431_miseq/R1/MISEQ_431_Undetermined_S0_R1_001.fastq.gz
/lustre/scratch119/realdata/mdt1/team113/projects/im13_basespace_runs/FASTQ/241816583_walkup_194_hiseq_2500_repeat/R1/HISEQ_194_REPEAT_A01_FR_KAPA_25x_1ug_SR_1ngx4rxns_S1_R1_001.fastq.gz
/lustre/scratch119/realdata/mdt1/team113/projects/im13_basespace_runs/FASTQ/241816583_walkup_194_hiseq_2500_repeat/R1/HISEQ_194_REPEAT_A02_FR_KAPA_25x_2ug_SR_1ngx4rxns_S2_R1_001.fastq.gz
/lustre/scratch119/realdata/mdt1/team113/projects/im13_basespace_runs/FASTQ/241816583_walkup_194_hiseq_2500_repeat/R1/HISEQ_194_REPEAT_A03_FR_KAPA_25x_3ug_SR_1ngx4rxns_S3_R1_001.fastq.gz
/lustre/scratch119/realdata/mdt1/team113/projects/im13_basespace_runs/FASTQ/241816583_walkup_194_hiseq_2500_repeat/R1/HISEQ_194_REPEAT_A06_FR_KAPA_23x_3ug_SR_1ngx14rxns_S4_R1_001.fastq.gz
/lustre/scratch119/realdata/mdt1/team113/projects/im13_basespace_runs/FASTQ/241816583_walkup_194_hiseq_2500_repeat/R1/HISEQ_194_REPEAT_A10_FR_TAKARA_25x_1ug_SR_1ngx4rxns_S5_R1_001.fastq.gz
/lustre/scratch119/realdata/mdt1/team113/projects/im13_basespace_runs/FASTQ/241816583_walkup_194_hiseq_2500_repeat/R1/HISEQ_194_REPEAT_A11_FR_TAKARA_25x_2ug_SR_1ngx4rxns_S6_R1_001.fastq.gz
/lustre/scratch119/realdata/mdt1/team113/projects/im13_basespace_runs/FASTQ/241816583_walkup_194_hiseq_2500_repeat/R1/HISEQ_194_REPEAT_A12_FR_TAKARA_25x_3ug_SR_1ngx4rxns_S7_R1_001.fastq.gz
/lustre/scratch119/realdata/mdt1/team113/projects/im13_basespace_runs/FASTQ/241816583_walkup_194_hiseq_2500_repeat/R1/HISEQ_194_REPEAT_B03_FR_TAKARA_25x_3ug_SR_1ngx12rxns_S8_R1_001.fastq.gz
/lustre/scratch119/realdata/mdt1/team113/projects/im13_basespace_runs/FASTQ/241816583_walkup_194_hiseq_2500_repeat/R1/HISEQ_194_REPEAT_B05_Plasmid_DNA_KAPA_15x_5ng_SR_40pgx4rxns_S9_R1_001.fastq.gz
/lustre/scratch119/realdata/mdt1/team113/projects/im13_basespace_runs/FASTQ/241816583_walkup_194_hiseq_2500_repeat/R1/HISEQ_194_REPEAT_Undetermined_S0_R1_001.fastq.gz
For each file path, I would like to extract just:
HISEQ_194_REPEAT_A01_FR_KAPA_25x_1ug_SR_1ngx4rxns_S1
HISEQ_194_REPEAT_A02_FR_KAPA_25x_2ug_SR_1ngx4rxns_S2
etc.
How is this possible? Thanks
This worked to get the result I required:
for FILE_PATH in $FASTQ_DIR/R1/*.fastq.gz
do
FILE_NAME="$(basename "$FILE_PATH")"
SAMPLE=${FILE_NAME%_R1_001.fastq.gz}
done
Need to print last directory from following example. Directory structures looks like below:
"/home/user/example/bar"
"/home/user/example/backup/foo"
"/home/user/example/bar1"
"/home/user/example/backup/tmp/bar2"
"/home/user/example/bar3"
"/home/user/example/bar4"
ONLY IF my complete directory path contains "example" directory then I need the last directory (bar , foo, bar1, bar2, bar3, bar4) into some variable.
How can I always detect the last directory (bar , foo, bar1, bar2, bar3, bar4) like the example above?
Also If my directory path contains example then I need to fetch the immediate directory after the example.
"/home/user/example/backup/foo" result should be backup
"/home/user/example/bar1" result should be bar1
"/home/user/example/backup/tmp/bar2" result should be backup
"/home/user/example/bar3" result should be bar3
"/home/user/example/bar4" result should be bar4
Thanks in advance! :)
Try the following:
DIR="/home/user/example/bar3"
if [ "${DIR##*/example/*}" = "" ]; then
echo "Contains example"
LAST_DIR="${DIR##*/}"
echo "Last dir is: ${LAST_DIR}"
fi
I am trying to iterate over a fish shell list and change the values of the elements saving those new values into that list.
The original list is populated like this:
set -l dockApplications (ls $HOME/.config/plank/dock1/launchers/)
This works and produces a list like this:
emacsclient.dockitem firefox.dockitem monodevelop.dockitem Thunar.dockitem
Now I want to iterate over it and change the string "dockitem" to "desktop".
I have tried a for loop but I do not appear to be using it correctly:
for application in $dockApplications
echo $application
set application (string replace 'dockitem' 'desktop' $application )
echo $application
echo "==========="
end
This echos the before and after the string operation and I produce the correct string. but when I do something like echo $dockApplications after the for loop. I get the list of strings with the "dockitem" extension.
I have also tried setting the $dockApplications variable differently like:
set -l dockApplications (string replace 'dockitem' 'desktop' (ls $HOME/.config/plank/$dockdir/launchers/))
But that seems to return the same list of strings with the "dockitem" extension.
I think I am fundamentally misunderstanding either how variables are assigned or how the scope of them is handled here.
I have never fully wrapped my head around shell scripting. I can read it and get what is going on but when it comes to implementing it I hit a few road blocks when I try to achieve something in a way that I would in a different language. But I realize the power of shell scripting and would like to get better at it. So any pointers on how to do this in a fish shell idiomatic way are greatly appreciated.
I would iterate over the indices of the list so you can update in place:
set apps (printf "%s\n" ./launchers/*.dockitem)
for i in (seq (count $apps))
set apps[$i] (string replace "dockitem" "desktop" $apps[$i])
end
printf "%s\n" $apps
This also works without a loop:
set apps (string replace dockitem desktop (printf "%s\n" ./launchers/*.dockitem))
printf "%s\n" $apps
If I recall, fish splits the output of a command substitution on newlines when saving to an array
It may not be the most elegant solution, but here is a pure fish way of doing this using the venerable string function.
set mystring "Lorem ipsum dolor sit amet, consectetur adipisicing elit"
set mywords (string split " " $mystring)
for i in (seq (count $mywords))
set mywords[$i] (string upper (string sub -s 1 -l 1 $mywords[$i]))(string sub -s 2 $mywords[$i])
end
echo $mywords
I have a directory containing files and they are all processed except one, file2.txt with my the_script.py script.
Independanty i ran a simple for line in file2.txt: print line and it worked just fine. The lines were printed. So the file is not the problem, it is formatted just as the other ones (automatically, output of another script).
Here is the_script.py :
#!/usr/bin/python
import os
import glob
#[...]rest of the code not dealing with the files in questions
for filename in glob.glob("outdir/*_mapp"): #i need to get all the files in outdir/ directory with the *_mapp extension
infilemapp=open(filename)
print "start"
print infilemapp #test, priting all filenames
organism=(filename.split("/", 1)[1])[:-5] # outdir/acorus.txt_mapp --> acorus.txt IRRELEVANT PARSING LINE
infilelpwe=organism+"_lpwe" #acorus.txt --> acorus.txt_lpwe IRRELEVANT PARSING LINE
for line in infilemapp:
print line
print "end"
What i expected is to get, for ALL files, "start, filename, filecontent, end". I get in console:
bash-4.3$ ./the_script.py
start
<open file 'outdir/file1.txt_mapp', mode 'r' at 0x7fb5795ec930>
['3R', '2F', '0R', '3F', '1R', '4F', '1F']
end
start
<open file 'outdir/file3.txt_mapp', mode 'r' at 0x7fb5795eca50>
['0R', '5R', '7R', '4R', '1F', '6R', '2R', '6F', '1R', '4F', '7F', '5F', '0F', '3R']
end
start
<open file 'outdir/file2.txt_mapp', mode 'r' at 0x7fb5795ec930>
end
As you can see, nothing is printed for file2.txt_mapp.
bash-4.3$ cat outdir/file2.txt_mapp
['5F', '0F', '2F', '6F', '3R', '5R', '6R', '4F', '1R', '4R', '6F']
The file is alphabetically in the middle of all files. Why does my script not work for this specific one? Please if you have any suggestions...
I get an ambiguous redirect message even though the output file gets created.
my sh script
#!/bin/bash
# you can use read or VAR="$1" to setup these variables
SERVER_IP=
SERVER_PORT=
LANGUAGE_URL=
PROJECT_NAME=
while read f1
do
OUTPUTFIL=$f1
{
echo "<?xml version=\"1.0\" encoding=\"Shift-JIS\"?>"
echo "<flash_cfg>"
echo "<server ip=\"${SERVER_IP}\" port=\"${SERVER_PORT}\"/>"
echo "<language_url>${LANGUAGE_URL}</language_url>"
echo "<project_name>${PROJECT_NAME}</project_name>"
echo "</flash_cfg>"
} > ${OUTPUTFIL}
done < file
content of "file
out.xml
while running
:~/Documents$ bash shell.sh
shell.sh: line 22: ${OUTPUTFIL}: ambiguous redirect
The file out.xml is created however
No contradiction there, you have a loop.
So first you read a valid filename (out.xml), and create a file, then you're reading an invalid one, which creates the error message.
Example (you have an empty line in the input):
f=""
echo "Q" > ${f}
-bash: ${f}: ambiguous redirect
I'd use cat to simplify the code--see if this works any better:
while read f1
do
cat <<EOF >"$f1"
<?xml version="1.0" encoding="Shift-JIS"?>
<flash_cfg>
<server ip="${SERVER_IP}" port="${SERVER_PORT}"/>
<language_url>${LANGUAGE_URL}</language_url>
<project_name>${PROJECT_NAME}</project_name>
</flash_cfg>
EOF
done < file
That's known as a "here document" and lets you avoid all those echo's and quoting.