In output, echo command printed new line when after printed variable - linux

This is my small bash script code and i want to print the number of files created in the directory :
#! /bin/sh
home_dir=/home/var/foo
Count= ls /$home_dir|wc -l
echo -e "$Count files are existed in the directory $home_dir"
exit 0
My expected output is :
9 files are existed in the directory /home/var/foo
but i got the below output:
9
files are existed in the directory /home/var/foo
can you help what went wrong in my above code? Also please suggest if this is the correct way to print the number of files in the directory

This works for me:
Count=$(ls /$xml_dir|wc -l)

To print on the same line
echo -ne "$Count files are existed in the directory $home_dir"
Add argument n to the echo.

Related

Issues trying to execute bash script on windows using cygwin

I'm trying to run a bash script on Windows 7, using cygwin. The script takes two lists of file destinations (files are the same sprinkled in different pairs of folders), iterates through them and detects if the files changed.
#!/bin/bash
src=(
"./src/index.js"
"./src/index_2.js"
)
dest=(
"./client/src/index.js"
"./client/src/index_2.js"
)
arraylength=${#src[#]};
for (( i=0; i<${arraylength}; i++ ));
do
DIFF=$(diff -u ${src[$i]} ${dest[$i]})
if [ $? != 0 ]; then
echo "$DIFF"
echo "Files ${src[$i]} and ${dest[$i]} are not equal!"
exit 1
fi
done
echo "All files are equal"
When I run the command like ./shareddiff.sh, the command executes without errors, but displays nothing (no echo message). Even when I manualy change one of the index.js or index_2.js files - it doesn't detect the change.
Any idea what I could be doing wrong?
You are misusing diff in passing the file arguments; you can compare two files or two directories, not two list of files.
SYNOPSIS
diff [OPTION]... FILES
FILES are 'FILE1 FILE2' or 'DIR1 DIR2' or 'DIR FILE' or 'FILE DIR'. If
--from-file or --to-file is given, there are no restrictions on
FILE(s). If a FILE is '-', read standard input. Exit status is 0 if
inputs are the same, 1 if different, 2 if trouble.
try
diff -uR src client/src

Reading specifed file line and creating new directories from words that have been taking of that file

for file in $*
head -n 1 $file | while read folder
do
mkdir $directory $folder
done
Hello guys, I'm having problem with my script. What I want to do is: read first line from my specifed file and create new directories in my specifed directory from words that i have taken from that file.
I'm getting errors like this:
./scriptas: line 2: syntax error near unexpected token `head'
./scriptas: line 2: `head -n 1 $file | while read folder'
And my second question: how do I add a second variable from command line (putty) $directory ?
Example i have file with text:
one two three
five seven nine eleven
okey
i need script to take the first line and create directories "one" "two" "three"
You have to put do before the command in a for/while cycle.
Your code should look like something like this:
#!/bin/bash
files=$*
for file in $files
do
head -n1 "$file" | while read dname
do
mkdir $dname
done
done
as for other variables, the simple syntax is a number behind the $ sign.
so you could do
files="$1"
directory="$2"
and then run the script as
./script.sh "file1.txt file2.txt file3.txt" dir2
More complex solutions include getopts and such....
Updated the script. You can use it in this way:
script.sh "one.txt two.txt three.txt" destdir
#! /bin/bash
for files in $1
do
for i in $(head -n 1 $files)
do
if [ -z $2 ]
then
mkdir $i
else
mkdir $2/$i -p
fi
done
done

Linux shell script to copy last lines of a file in a directory to append to a file in another directory

I have written the below shell script to copy the last lines of a file in a directory to append to a file in another directory
cd /opt/grinder/svn/IAVS/GrinderLogs/GrinderBaseLogs/Lg2Logs
GrinderLG1='rm101sys1lweb22'
GrinderLg2='rm101sys1lweb23'
fileCount=$(ls -l|wc -l)
echo $fileCount
for (( c=0; c<=$fileCount-2; c++ ))
do
Lines=$(more $GrinderLg2"-"$c"-data.log"|wc -l)
Lines1=`expr $Lines - 1`
`"tail -"$Lines1"f /opt/grinder/svn/IAVS/GrinderLogs/GrinderBaseLogs/Lg2Logs/"$GrinderLg2"-"$c"-data.log>>/opt/grinder/svn/IAVS/GrinderLogs/GrinderBaseLogs/"$GrinderLG1"-"$c"-data.log"`
#exec $command
done
When I am executing this script it says no such file or directory at tail command. Actually both the files exist. Please help.
`"tail -"$Lines1"f /opt/grinder/svn/IAVS/GrinderLogs/GrinderBaseLogs/Lg2Logs/"$GrinderLg2"-"$c"-data.log>>/opt/grinder/svn/IAVS/GrinderLogs/GrinderBaseLogs/"$GrinderLG1"-"$c"-data.log"`
change to
tail -"$Lines1"f /opt/grinder/svn/IAVS/GrinderLogs/GrinderBaseLogs/Lg2Logs/"$GrinderLg2"-"$c"-data.log>>/opt/grinder/svn/IAVS/GrinderLogs/GrinderBaseLogs/"$GrinderLG1"-"$c"-data.log
should work.
In my setup i have tested as below way
#!/bin/sh
`"tail -10f filename"`
give error filename not found
But
#!/bin/sh
tail -10f filename
This works fine.

Storing directory as a variable for later use in linux script

In my script, I am holding the location (path) of a file as a variable.
For example, fileA
An example of its contents are
fileA=/usr/anotherfolder/somefold/"filenamehere"
However, when i call a command on the file in the script such as:
cat $fileA
or
cat "$fileA"
I get an error saying the file or directory doesn't exist. If I echo $fileA to see what the output is, and then run a cat manually from the terminal, it works fine, don't know what is going wrong. Any help?
Some debug info:
fileA='/home/jacob/Desktop/CS35L/WORK/2/hw/test3/"new"'
echo '/home/jacob/Desktop/CS35L/WORK/2/hw/test3/"new"'
/home/jacob/Desktop/CS35L/WORK/2/hw/test3/"new"
'[' '!' -r '/home/jacob/Desktop/CS35L/WORK/2/hw/test3/"new"' ']'
For these particular lines
Check for readable file
echo $fileA
if [ ! -r "$fileA" ]
then
o=`expr $o + 1`
echo "$fileA not readable."
continue
fi
If file name is new(not "new"), then change
fileA='/home/jacob/Desktop/CS35L/WORK/2/hw/test3/"new"'
to
fileA=/home/jacob/Desktop/CS35L/WORK/2/hw/test3/new

How to process file names with variables from a list in a file in Bash

I have a file "FileList.txt" with this text:
/home/myusername/file1.txt
~/file2.txt
${HOME}/file3.txt
All 3 files exist in my home directory. I want to process each file in the list from a bash script. Here is a simplified example:
LIST=`cat FileList.txt`
for file in $LIST
do
echo $file
ls $file
done
When I run the script, I get this output:
/home/myusername/file1.txt
/home/myusername/file1.txt
~/file2.txt
ls: ~/file2.txt: No such file or directory
${HOME}/file3.txt
ls: ${HOME}/file3.txt: No such file or directory
As you can see, file1.txt works fine. But the other 2 files do not work. I think it is because the "${HOME}" variable does not get resolved to "/home/myusername/". I have tried lots of things with no success, does anyone know how to fix this?
Thanks,
-Ben
Use eval:
while read file ; do
eval echo $file
eval ls $file
done < FileList.txt
From the bash manpage regarding the eval command:
The args are read and concatenated together into a single command. This command is
then read and executed by the shell, and its exit status is returned as the value of
eval. If there are no args, or only null arguments, eval returns 0.
you will hit "spaces problem" using the for loop with cat. Manipulate IFS, or use a while read loop instead
while read -r line; do eval ls "$line"; done < file
Change "ls $file" to "eval ls $file" to get the shell to do its expansion.

Resources