I had a directory with 10 files.I need to read each file one by one .If any failure while processing the file then I need to capture that file name to send over the mail and skip the current file and move to the next file in the directory.
I tried like this
for test in $test_path
do
if [ ! -s $test ];then
echo ' failed'
fi
#other codes are here that needs to run with the above input file
done
I'm new to Linux shell script and I'm struggling with a problem. An error pops up telling that the if conditional has too many arguments. What I have to do is basically described on the title, but I've written a code that is not working, what's wrong with it? The original directory is called artists and the subdirectory where the files need to be copied to is called artists_copy.
#!/bin/bash
count=0
elem=$(ls)
for file in $elem; do
let count+=1
done
for i in {$count}; do
if [ -e $elem[$i] ]; then
cp $elem[$i] artists_copy
echo "Copied file $elem[$i] to artists_copy"
fi
done
I am trying to create a script in Linux that will prompt the user to input the name of a file and according to the user's input it will create the file and put the following in the file "This is a file (filename)."
This is my attempt so far.
cat > hw5_script
echo "Enter filename:"
read filename
echo "This is a file filename"
This doesn't work for me. Just wondering what I am doing wrong? Any suggestions or input would be greatly appreciated.
You're not redirecting the output of the script to the file, and you're not using $ to expand the variable. Change the last line of the script to:
echo "This is a file $filename" > "$filename"
I'm trying to redirect(?) my standard error/output to a text file.
I did my research, but for some reason the online answers are not working for me.
What am I doing wrong?
cd /home/user1/lists/
for dir in $(ls)
do
(
echo | $dir > /root/user1/$dir" "log.txt
) > /root/Desktop/Logs/Update.log
done
I also tried
2> /root/Desktop/Logs/Update.log
1> /root/Desktop/Logs/Update.log
&> /root/Desktop/Logs/Update.log
None of these work for me :(
Help please!
Try this for the basics:
echo hello >> log.txt 2>&1
Could be read as: echo the word hello, redirecting and appending STDOUT to the file log.txt. STDERR (file descriptor 2) is redirected to wherever STDOUT is being pointed. Note that STDOUT is the default and thus there is no "1" in front of the ">>". Works on the current line only.
To redirect and append all output and error of all commands in a script, put this line near the top. It will be in effect for the length of the script instead of doing it on each line:
exec >>log.txt 2>&1
If you are trying to obtain a list of the files in /home/user1/lists, you do not need a loop at all:
ls /home/usr1/lists/ >Update.log
If you are attempting to run every file in the directory as an executable with a newline as its input, and collect the output from all these programs in Update.log, try this:
for file in /home/user1/lists/*; do
echo | "$file"
done >Update.log
(Notice how we avoid the useless use of ls and how there is no redirection inside the loop.)
If you want to create an empty file called *.log.txt for each file in the directory, you would do
for file in /home/user1/lists/*; do
touch "$(basename "$file")"log.txt
done
(Using basename to obtain the file name without the directory part avoids the cd but you could do it the other way around. Generally, we tend to avoid changing the directory in scripts, so that the tool can be run from anywhere and generate output in the current directory.)
If you want to create a file containing a single newline, regardless of whether it already exists or not,
for file in /home/user1/lists/*; do
echo >"$(basename "$file")"log.txt
done
In your original program, you redirect the echo inside the loop, which means that the redirection after done will not receive any output at all, so the created file will be empty.
These are somewhat wild guesses at what you might actually be trying to accomplish, but should hopefully help nudge you slightly in the right direction. (This should properly be a comment, I suppose, but it's way too long and complex.)
I'm trying to figure out how to add content/code to the end of a .js file that already has code in it using ssh command.
ie....
touch ./ap/includes/ckeditor/ckeditor.js
Maintain current code
echo "add custom end code only"> ./ap/includes/ckeditor/ckeditor.js
sshcommand is used to connect to another server.
What you can do append text to the end of a file is to echo "something" >> /your/file.
So based on your code:
touch ./ap/includes/ckeditor/ckeditor.js
Maintain current code
echo "add custom end code only" >> ./ap/includes/ckeditor/ckeditor.js
^
|_ changed this
By the way, the touch part is unnecessary. When echoing inside the file, the date of the file will be updated. And if file does not exist, it will be automatically created with echo.