I'm trying to make a bash script to add a description line to a .htaccess file.
Specifically, I want to take two inputs where one is the description and the other is the file path. All of this has to go after AddDescription. I need this to output one string that I can then add to a file.
All together, it should come out to something like this:
AddDescription description path
How can I do it?
with echo you can achieve this
echo AddEscription $var $PATH
if you want to write a script for this, there is no need to specify number of arguments. Create an executable file with these contents and run with as many arguments as you want
#!/bin/bash
echo AddEscription "$#"
Try
RESULT=$(paste <(echo "AddDescription") <(echo "$DESCRIPTION_VAR") <(echo "$PATH_VAR") -d ' ')
and then
echo $RESULT
assuming that you assigned your first input to $DESCRIPTION_VAR and your second input to $PATH_VAR.
Related
I have a variable that contains a string-
$CCSR = "branches/features/arm_and_musl"
I want to pass only the "arm_and_musl" part to a variable, while excluding "branches/features", so something like this-
def dirname= sh " echo $CCSR | grep ????? "
But the main issue is that I don't want to explicitly mention "arm_and_musl" part, I want it to just exclude "branches/features" and print the remaining part whatever that may be.
I'm not sure what to put here so that only the part I want is passed to the variable.
Could you please suggest any solutions for this?
You could use something like this:
echo ${CCSR##*/}
to just output the needed section or if you need the variable to contain it use:
CCSR=${CCSR##*/}
Edit:
Since for some reason author was getting an error, thought i would include a solution with sed:
echo $CCSR | sed "s:.*/::"
Just use basename:
#!/bin/sh
CCSR="branches/features/arm_and_musl"
file=$(basename "$CCSR")
echo "$file"
Here is some bash code
path='*'
echo $path
If I execute the code in terminal directly, the output is
*
But if I put it in a shell script e.g., test.sh. Then I execute the test.sh, the output will be like this
test.sh file1 file2 file3
I got a string contains all items under the path.
Why the two outputs is different? If I want the second output to be the first one, which means do not trans a path string when I use it, what should I do?
I tested a little. You could change your script to this:
path='*'
echo "$path"
and it will print * instead of files if you run it as a bash script.
Seems echo can be used as a alternative of ls command:
echo * means list all files under current folder.
echo *.jpg will list all files with jpg as suffix, and if there is no such file in current folder, echo *.jpg will just print "*.jpg".
I think this answer should be posted as a comment, since I don't really know why echo command behave like this. But I do not have enough reputation to add comment, so...
I want to create empty files where the names are taken from list.txt. I tried this:
declare -a siglist=$(cat list.txt)
for fname in {list}; do
echo $fname > "$fname.zz"
done
Note that your echo solution does not create empty files: they contain a single newline.
If you want to stay entirely in the shell (the comments show various ways using touch and xargs), you can use the null-command : (which does nothing) with a redirection as follows.
while read filename; do
: > "${filename}.zz"
done < list.txt
This is portable to all Bourne-heritage shells, so not restricted to bash.
If you are ok with awk, could you please try following.
awk '{val=(val?val OFS:"")$0} END{system("touch " val)}' Input_file
Explanation: Creating a variable named val in awk program and keep concatenating line's values into it. In END section of this awk program using system command of this command to use touch command and passing that variable val to it to create all file names there.
I have this script:
#!/bin/bash
FASTQFILES=~/Programs/ncbi-blast-2.2.29+/DB_files/*.fastq
FASTAFILES=~/Programs/ncbi-blast-2.2.29+/DB_files/*.fasta
clear
for file in $FASTQFILES
do cat $FASTQFILES | perl -e '$i=0;while(<>){if(/^\#/&&$i==0){s/^\#/\>/;print;}elsif($i==1){print;$i=-3}$i++;}' > ~/Programs/ncbi-blast-2.2.29+/DB_files/"${FASTQFILES%.*}.fasta"
mv $FASTAFILES ~/Programs/ncbi-blast-2.2.29+/db/
done
I'm trying it to grab the files defined in $FASTQFILES, do the .fastq to .fasta conversion, name the output with the same filename of the input, and move it to a new folder. E.g., ~/./DB_files/HELLO.fastq should give a converted ~/./db/HELLO.fasta
The problem is that the output of the conversion is a properly formatted hidden file called .fasta in the first folder instead of the expected one named HELLO.fasta. So there is nothing to mv. I think I'm messing up in the ${FASTQFILES%.*}.fasta argument but I can't seem to fix it.
I see three problems:
One part of your trouble is that you use cat $FASTQFILES instead of cat $file.
You also need to fix the I/O redirection at the end of that line to > ~/Programs/ncbi-blast-2.2.29+/DB_files/"${file%.fastq}.fasta".
The mv command needs to be executed outside the loop.
In fact, when processing a single file at a time, you don't need to use cat at all (UUOC — Useless Use Of Cat). Simply provide "$file" as an argument to the Perl script.
So I am trying to write to a file and I want my file name to be the time when I opened it. I have something like this:
var=$(date +"%D-%H:%M:%S")
echo "I opened a file" > $var
cat $var
When I try to run this code, it gives me the error:
NO SUCH FILE OR DIRECTORY
Can someone tell me what is going on?
You should use quotes in your variable while using it:
var="$(date +"%d-%m-%Y-%H:%M:%S")"
echo "I opened a file" > "$var"
cat "$var"
EDIT: Your problem is use of slash / in your date variable that makes shell thinks that it is a path rather than a file name. You will need to change your date format as I suggested above.
That's because your redirect is to the file name ':' and the rest of the filename are being wordsplit into following parameters. Use more quotes.
The other problem is that you cannot create a filename with slashes in it. Of course, that's a directory structure. You will need to replace the slashes with something else. You can do this by modifying the date command, but it's also easy to use parameter expansion, as I show below:
var=$(date +"%D-%H:%M:%S")
$ echo "$var"
03/12/14-15:20:46
$ echo 'data' > "${var//\//_}" # replace slashes with underscores
If you're just trying to create datestamped files, consider using the epoch timestamp:
var=$(date +%s)
echo "$somedata" > "$var"
It's easy for a machine to convert values to/from the epoch timestamp, and it's easier for you and most users to deal with a filename made up of a string of digits than a human-readable timestamp.