I want a linux cmd to write the value of a variable into a file. Heres what i have,
x=$(cat /home/kate/Documents/Desktop/New-ACE-Deploy/deploy/ace/deploysetup/ConfFiles/online_ace_stable-m4.5.conf)
echo $x
cd /etc/apache2/sites-enabled
cat > online_ace_stable-m4.5.conf
echo "$x" >> "/etc/apache2/sites-enabled/online_ace_stable-m4.5.conf"
But i have to press Ctrl +D to end. I dont want to do it. Any alternatives.
Remove the cat > online_ace_stable-m4.5.conf line and you should be fine with regards to pressing Ctrl+D. If the file may exist before you run the script, you want to additionally replace >> (appending) on the last line with > (overwriting).
If you really need to explicitly make sure the file exists and is empty, use echo -n > online_ace_stable-m4.5.conf.
Your cat command on the 4th line is incorrect. It is missing a file parameter. Without this parameter is copies stdin to stdout. This goes on until stdin is closed, hence you need to use CTRL-D
In order to fix your problem, change the line it to:
cat online_ace_stable-m4.5.conf
(Note the > is gone)
If you want to create an empty (new) file, use touch instead of cat:
touch online_ace_stable-m4.5.conf
or directly echo to the file:
echo "$x" > "/etc/apache2/sites-enabled/online_ace_stable-m4.5.conf"
as it it not require to first create it and make sure that old contents are not kept. (Note the single > instead of the double >>)
Related
Using at to schedule a systems message. I need to run it in a script, so the first option which requires ctrl+d is not viable. Also note, in the first option, the pipe fails UNLESS I add a nonsense echo statement below it.
first option, works but cannot use:
$ at now
> cat file.txt | write user pts/0
> echo "nonsense"
>[ctrl+d]
second option, which needs to work:
$ at now << 'END_AT'
> cat file.txt | write user pts/0
> END_AT
Please note that with or without the nonsense echo in the second option, it fails to cat the contents of the text file. I need this second option to properly display, what am I doing wrong here??
I want to save a command to a file (for example I want to save the string "cat /etc/passwd" to a file) but I can't use the echo command.
How can I create and save string to a file directly without using echo command?
You can redirect cat to a file, type the text, and press Control-D when you're done, like this:
cat > file.txt
some text
some more text
^D
By ^D I mean to press Control-D at the end. The line must be empty.
It will not be part of the file, it is just to terminate the input.
Are you avoiding ECHO for security purposes (e.g. you're using a shared terminal and you don't want to leave trace in the shell history of what you've written inside your files) or you're just curious for an alternative method?
Simple alternative to echo:
As someone said, redirecting cat is probably the simpler way to go.
I'd suggest you to manually type your end-of-file, like this:
cat <<EOF > outputfile
> type here
> your
> text
> and finish it with
> EOF
Here's the string you're asking for, as an example:
cat <<EOF > myscript.sh
cat /etc/passwd
EOF
You probably don't want everyone to know you've peeked into that file, but if that's your purpose please notice that wrapping it inside an executable file won't make it more private, as that lines will be logged anyway...
Security - Avoiding history logs etc..
In modern shell, just try adding a space at the beginning of every command and use freely whatever you want.
BTW, my best hint is to avoid using that terminal at all, if you can. If you got two shells (another machine or even just another secure user in the same machine), I'd recommend you using netcat. See here: http://www.thegeekstuff.com/2012/04/nc-command-examples/?utm_source=feedburner
{ { command ls $(dirname $(which cat)) |
grep ^ca't$'; ls /etc/passwd; } |
tr \\n ' '; printf '\n'; } > output-file
But it's probably a lot simpler to just do : printf 'cat /etc/passwd\n'
To be clear, this is a tongue-in-cheek solution. The initial command is an extraordinarily convoluted way to get what you want, and this is intended to be a humorous answer. Perhaps instructive to understand.
I am not sure I understood you correctly but
cat /etc/passwd > target.file
use the > operator to write it to file without echoing
If you need to use it, inside a program :
cat <<EOF >file.txt
some text
some more text
EOF
I would imagine that you are probably trying to print the content of a string to a file, hence you mentioned echo.
You are avoiding this:
echo "cat /etc/passwd" > target.file
You can use a here string combined with cat.
cat > target.file <<< "cat /etc/passwd"
Now the file target.file will contain a string cat /etc/passwd.
$ cat target.file
cat /etc/passwd
$
To create string:
var1=your command
to save a file or variable in a file without echo use:
cat $FILE/VAR1 > /new/file/path
I run the following code for concatenating files in a directory given as the argument for the script file in bash
for i in $*
do
cat $* > /home/christy/Documents/filetest/catted.txt
done
This produce the error
cat: /home/christy/Documents/filetest/catted.txt: input file is output file
I think there are at least 4 things wrong with your script....
Firstly, your loop will set the value of i to the name of each file in succession, so you would want to actually use i inside your loop, like this:
for i in $*
cat "$i" ....somewhere
done
Secondly, if you use the > redirection, each file will land exactly on top of the previous one, so you should really use the >> redirection will append the current file to the end of the previous one like this
for i in $*
do
cat "$i" >> ...somewhere
done
Thirdly, I think you should use double-quoted "$#" to get all your command-line arguments, rather than plain $*
for i in "$#"
...
Fourthly, you can achieve the exact effect I think you want with this simpler command:
cat "$#" > /home/christy/Documents/filetest/catted.txt
You can't cat a file back onto itself. That's what "input file is output file" means. Because catted.txt shows up in your list of arguments to cat, it is going to try to cat to itself. So, move catted.txt to somewhere other than the source directory.
I'm trying to do the following :
cat > somefile "some text" <ctrl+d>; clear; some other program
but without having to press
<"ctrl + d">
so that line will create the file and then run some other program. I tried echo "some text" > somefile; but there are too many special chars for that. Anyone know a way around this
I think what you may be looking for is something along these lines:
pax> cat >tempfile ; tput clear ; someprog
Enter your data here for tempfile
<ctrl-d>
**screen clears and someprog runs**
The end-file CTRL-D isn't part of the command you enter, it's part of the input stream for the cat command.
If you don't want to use the input stream, you're either going to have to work out the echo variant (learn to embrace the wonderful world of shell escapes - you can get around most of them by just using single quotes instead of double ones), or simply create your input file in advance and use something like:
cp sourcefile tempfile ; tput clear ; someprog
If you wish to write some text in somefile in multiple lines or with special characters, you should try this. EOF is treated as a special string here that will terminate cat automatically when found but it could be anything else.
cat > somefile << EOF
some text
EOF
some other program
I'm trying to write a pretty basic script in Linux shell but I'm still learning. Basically, everything is good to go except one part. I direct two outputs into the same file, e.g.:
echo `losetup -a` > partitionfile
echo "p1" >> partition final
Basically, I need to add the letter/number "p1" to the end of whatever is written in the file.
The problem is, it ends up being read (cat partitionfile) as:
/dev/loop0
p1
I need it on the same line to it reads out as:
/dev/loop0p1
There has to be a way to fix this, I just don't know it. Any help would be much appreciated!
Thanks!
I would go for:
echo "$(losetup -a)p1" > partitionfile
For an example, see the following transcript:
pax> echo "$(echo xyzzy_)p1"
xyzzy_p1
The xyzzy_ is the output of the inner echo command (which in your case would be losetup) and the outer echo command appends p1.
Hi Actually the correct option of echo to achieve this is "\c"
\c Keeps the cursor on the same line.
However you cannot use \c unless you have enabled it with
-e
Thus your code should be something like this ...
echo -e "`losetup -a` \c" > partitionfile
echo "p1" >> partition final
this will write in partitionfile as
< output of losetup -a > p1
everything on same line.
You can pass -n flag to the first echo statement to not print the trailing new line.
Ref: http://linux.die.net/man/1/echo