Escribir en el archivo, pero sobrescribirlo si existe
echo "text" >> 'Users/Name/Desktop/TheAccount.txt'
Cómo lo hago para que cree el archivo si no existe, pero lo sobrescribe si ya existe. Ahora mismo este script solo se añade.
6 answers
Un poco de comprensión de cómo funcionan las tuberías *nix ayudaría.
En resumen, el operador de redirección >>
añadirá líneas al final del archivo especificado, donde-como el único mayor que >
vaciará y sobrescribirá el archivo.
echo "text" > 'Users/Name/Desktop/TheAccount.txt'
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-11-07 19:23:21
En Bash, si ha establecido noclobber a la set -o noclobber
, entonces utiliza la sintaxis >|
Por ejemplo:
echo "some text" >| existing_file
Esto también funciona si el archivo aún no existe
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-11-20 09:10:12
A pesar de NylonSmile
's respuesta , que es "más o menos" correcta.. No pudesobrescribir los archivos de esta manera..
echo "i know about Pipes, girlfriend" > thatAnswer
zsh: file exists: thatAnswer
Para resolver mis problemas.. Tuve que usar... >!
, á la ..
[[ $FORCE_IT == 'YES' ]] && echo "$@" >! "$X" || echo "$@" > "$X"
Obviamente, ten cuidado con esto...
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-05-23 12:03:03
#!/bin/bash
cat <<EOF > SampleFile
Put Some text here
Put some text here
Put some text here
EOF
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-12-05 19:55:12
Si su entorno no permite sobrescribir con >
, use pipe |
y tee
en su lugar de la siguiente manera:
echo "text" | tee 'Users/Name/Desktop/TheAccount.txt'
Tenga en cuenta que esto también se imprimirá en la salida estándar. En caso de que esto no sea deseado, puede redirigir la salida a /dev/null
de la siguiente manera:
echo "text" | tee 'Users/Name/Desktop/TheAccount.txt' > /dev/null
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-08-14 23:34:13
Si tiene una salida que puede tener errores, es posible que desee usar un ampersand y mayor que, de la siguiente manera:
my_task &> 'Users/Name/Desktop/task_output.log'
esto redirigirá tanto stderr como stdout al archivo de registro (en lugar de solo stdout).
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-12-21 17:11:34