Bash shell script file exists - linux

Hi my bash script says the that the first parameter passed into it doesn't exist as a file but it does. Please what have I done wrong?
to run the script and pass in two arguments from linux:
./concantenatefile.sh file1.txt file2.txt
#!/bin/bash
file1="$1"
if [ -e file1 ]
then
echo "The file $file1 exists"
else
echo "The file $file1 doesn't exist"
fi
It says
The file file1.txt doesn't exist
However there is a file called file1.txt in the present working directory.

you are not currently derefencing the file1 variable
In the line
if [ -e file1 ]
You need to add a $ to derefence the variable and access its content as so
if [ -e $file1 ]

Related

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

Copy multiple files with bash script from command line arguments?

I want to create a script that allows me to enter multiple filenames from the command line, and have the script copy those files to another directory. This is what I am trying but I keep getting an error of
line 10: binary operator expected
#!/bin/bash
DIRECTORY=/.test_files
FILE=$*
if [ -e $DIRECTORY/$FILE ]; then
echo "File already exists"
else
cp $FILE $DIRECTORY
fi
So if the script was named copfiles.sh, I am writing...
./copyfiles.sh doc1.txt doc2.txt
It will move the files, but if they already exist, it won't read the error message.
Also I get the "line 10: binary operator expected" error regardless of it the files are there or not. Can anyone tell me what I am doing wrong?
As a possible problem, if you had a filename with a space or had multiple arguments $* would have spaces in it so [ -e $DIR/$FILE ] will expand to have lots of words, like [ -e /.test_files/First word and more ] and -e expects just 1 word after it. Try putting it in quotes like
if [ -e "$DIRECTORY/$FILE" ]
Of course, you may only want to store $1 in $FILE to get just the first argument.
To test all the arguments you want to loop over the arguments and test each with something like
for FILE in "$#"; do
if [ -e "$DIRECTORY/$FILE" ]; then
echo "$FILE already exists"
else
cp "$FILE" $DIRECTORY
fi
done
Using quotes around $# to preserve spaces in the original arguments as well

Create parameters for command Linux shell script

Hello I am trying to create parameters for my shell script but I am having trouble.
lets say for example the file is called test.
When I call ./test -parameter1 input_file.txt
I get an error saying 'no such file or directory'.
Here is an example of my code.
#!/bin/sh
if [ "$1" == "parameter" ]
then
while read line
do
#echo "$line"
done <$1
else
echo "Not working"
fi
My over all goal of this is to read in a file of numbers line by line which I have working, then to calculate the average values of the rows or by columns. Which is why I am trying to create parameters so the user will have to specify ./test -rows input_file.txt or ./test -columns input_file.txt
You are using the string -parameter as the input file name. Perhaps you want:
#!/bin/sh
if [ "$1" = "-parameter" ]
then
while read line
do
#echo "$line"
done <$2 # Use $2 instead of $1 here. Or use shift
else
echo "Not working" >&2
fi

How to read a file and copy from one file to another file in shell script

How to read a file and copy from one file to another file in shell script:
#!/bin/csh -f
echo ---file.txt---
cat file.txt
echo ######## file.text is opened ########
#set file_1="export/home/caratins/trial/file.txt"
while read line
do
echo "$line"
cp file.txt files
done<file.txt
Actually one folder trial is there, inside trial folder 4 text files are there. I want to open a file-'file.txt'. Inside file.txt 3 files names are there: test1.txt, test2.txt, test3.txt. My work is using file.txt file I have read all 3 files names and copy it to another folder. So for that I have to open file.txt, read the file and print 3 files and only copy these 3 files not full folder and copy these 3 files to another folder'files' which is in same directory.
if you want to copy entire file as it is then
cat filename >> newfilename
for three files
cat file1.txt file2.txt file3.txt >>file.txt
if you want to copy line by line then
while IFS= read -r line
do
echo "$line"
echo -e "$line\n" >>newfilename
done <"filename"
try this,
here test1 is you source folder, which will contail you files,
and test2 is destination folder where you will move your files after reading..
#!/bin/sh
cd test1;
echo "list of files:";
ls;
for filename in *;
do echo "file: ${filename}";
echo "reading..."
exec<${filename}
value=0
while read line
do
#value='expr ${value} +1';
echo ${line};
done
echo "read done for ${filename}";
cp ${filename} ../test2;
echo "file ${filename} moved to test2";
done
or you can try this...
ls;
echo "reading main file...";
filenames="filenames";
exec<${filenames}
while read name
do
echo "file: ${name}";
echo "reading..."
cd test1;
exec<${name}
value=0
while read line
do
#value='expr ${value} +1';
echo ${line};
done
echo "read done for ${name}";
cp ${name} ../test2;
cd ..;
echo "file ${file} moved to test2";
done
yo...

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

Resources