In a bash shell script how does one append read data to a csv file?
For example:
#!/bin/bash
echo "Enter name:"
read name
exit 0
If I want $name put into a .csv file from this script, how can this be done?
Maybe I'm missing something, but it sounds like all you need is
echo ${name} >> file.csv
The echo statement will send the value of name to standard output, and the >> will append the standard output to the end of file.csv.
Related
I am trying to send an output of an executed shell script, to a log file.
However I want to put a timestamp at the start of the line for every output, so I created a function to do that.
But how do I pass the results of the executed shell script, into the function?
#This is a sample of the executed file testrun.sh
#!/bin/bash
echo "Script Executed."
#Actual script being run
#!/bin/bash
testlog="/home/usr/testlog.log"
log_to_file() {
echo "$(date '+%Y-%m-%d %H:%M:%S') $1" >> $testlog
}
sh /home/usr/testrun.sh >> log_to_file
If i were to log it normally, i would just do
sh /home/usr/testrun.sh >> $testlog
But how do I pass in the output of testrun.sh, into the function log_to_file, so that I can log the output to the file with the timestamp?
You can of course do a
log_to_file "$(sh /home/usr/testrun.sh)"
Of course if your testrun.sh produces more than one line of output, only the first one gets the timestamp as prefix.
Use a while read loop to get each line into a variable that you can pass to log_to_file.
/home/usr/testrun.sh | while read -r line; do
log_to_file "$line"
done >> "$testlog"
You could also use the ts command instead of your function
/home/usr/testrun.sh | ts >> "$testlog"
Im writing a data formating bash script. The script reads the file name which is input by the user in the terminal. Of course, when no file under that name is found in that directory, the program ends with a stderr output. I am now trying to implement a (while) loop which recursively asks for user input until a matching file is found and then goes on to executing my data formating commands. Would appreciate some help :)
I am now trying to implement a (while) loop which recursively
I would advice against using recursion for this. Just make a simple loop. It could look like this:
while true; do
IFS= read -rp 'File: ' file
if [[ -e $file ]]; then
break;
fi
echo "$file doesn't exist, try again"
done
# work with $file here
How to write a shell script which creates a file
echo "Enter the file you want to create"
read a
touch $a
And adds content to the created file 'a'
echo "Enter the contents to your $a file:"
cat > $a << 'EOF'
EOF
The above one is not the right way, what I am doing wrong here ?
You were almost there. This will do what you ask:
echo "Enter the file you want to create"
read a
echo "Enter the contents to your $a file (press ctrl-D when done):"
cat >"$a"
Discussion
touch $a
It is not necessary to touch the file. The cat statement will create it regardless.
Also, because file names can include whitespace, it is best to put inside double-quotes any shell variable containing a file name. This prevents word splitting.
cat > $a << 'EOF'
EOF
The construct << 'EOF' tells the shell to read from a here document. This is what you use if you have text in your shell script that you want to use as input. You don't. You want instead to read input from the user. Thus the above two lines can be simply replaced with cat >"$a" .
You can ask then proceed with a simple echo:
echo "Enter the file you want to create"
read file
echo "Enter the contents to your $a file:"
read content
echo "$content" > "$file"
What I have to to is edit a script given to me that will check if the user has write permission for a file named journal-file in the user's home directory. The script should take appropriate actions if journal-file exists and the user does not have write permission to the file.
Here is what I have written so far:
if [ -w $HOME/journal-file ]
then
file=$HOME/journal-file
date >> file
echo -n "Enter name of person or group: "
read name
echo "$name" >> $file
echo >> $file
cat >> $file
echo "--------------------------------" >> $file
echo >> $file
exit 1
else
echo "You do not have write permission."
exit 1
fi
When I run the script it prompt me to input the name of the person/group, but after I press enter nothing happens. It just sits there allowing me to continue inputting stuff and doesn't continue past that part. Why is it doing this?
The statement:
cat >>$file
will read from standard input and write to the file. That means it will wait until you indicate end of file with something like CTRL-D. It's really no different from just typing cat at a command line and seeing that nothing happens until you enter something and it waits until you indicate end of file.
If you're trying to append another file to the output file, you need to specify its name, such as cat $HOME/myfile.txt >>$file.
If you're trying to get a blank line in there, use echo rather than cat, such as echo >>$file.
You also have a couple of other problems, the first being:
date >> file
since that will try to create a file called file (in your working directory). Use $file instead.
The second is the exit code of 1 in the case where what you're trying to do has succeeded. That may not be a problem now but someone using this at a later date may wonder why it seems to indicate failure always.
To be honest, I'm not really a big fan of the if ... then return else ... construct. I prefer fail-fast with less indentation and better grouping of output redirection, such as:
file=${HOME}/journal-file
if [[ ! -w ${file} ]] ; then
echo "You do not have write permission."
exit 1
fi
echo -n "Enter name of person or group: "
read name
(
date
echo "$name"
echo
echo "--------------------------------"
echo
) >>${file}
I believe that's far more readable and maintainable.
It's this line
cat >> $file
cat is concatenating input from standard input (ie whatever you type) to $file
I think the part
cat >> $file
copies everything from stdin to the file. Maybe if you hid Ctrl+D (end of file) the script can continue.
1) You better check first whether the file exists or not:
[[ -e $HOME/journal-file ]] || \
{ echo "$HOME/journal-file does not exist"; exit 1 }
2) You gotta change "cat >> $file" for whatever you want to do with the file. This is the command that is blocking the execution of the script.
I'm writing a shell script that asks for a value and depending on the valued entered returns a value. Imagine that it's called "reve" and contains something like
read fname
rev << EOF
$fname
EOF
Then if I have a file that is called "file.txt" and I do
vi `./reve`
Then waits for user input. If I type "txt.elif" vi opens "file.txt". Correct until now. But the problem is when the script is something like the following
echo "Enter inverted file name"
read fname
rev << EOF
$fname
EOF
Then it tries to open a file called "Enter inverted file name".
Is it possible to ask for the value with the text and after that use the returned value only?
Thanks in advance.
If you really need it to be interactive, then you could print the message on stderr instead.
echo "Enter inverted file name" > /dev/stderr
read fname
rev << EOF
$fname
EOF
Unless you're using a really old shell, read should have a -p option to supply a prompt. It automatically sends the prompt to stderr instead of stdout, and skips the linefeed (so the response is on the same line as the prompt):
read -p "Enter inverted file name: " fname
rev << EOF
$fname
EOF
Force writing to, and reading from, the terminal using /dev/tty.
In your example this will be:
echo -e "Enter inverted file name: \c" > /dev/tty
read fname < /dev/tty
rev << EOF
$fname
EOF
I used echo -e ... \c so no newline is printed and your input is entered on the same line.