mv: cannot stat ‘2001010000\r’: No such file or directory [duplicate] - linux

This question already has answers here:
Read a file line by line assigning the value to a variable [duplicate]
(10 answers)
Closed 2 years ago.
I have a file abc.out and I need to read it and export the content to the named pipe. My shell script as below:
#!/bin/ksh -x
cd /home/robert/
if [[ -s abc.out ]]; then
sleep 2
for i in `cat abc.out`
do
mv -f $i ./abc_archive/x$i
print `pwd`/abc_archive/x$i > /home/robert/pipe_abc
sleep 1
done
fi
The content of abc.out file as below:
2001010000
2001010001
2001010002
But when I run the script with the command sh abc.spt, error prompted as below:
mv: cannot stat ‘2001010000\r’: No such file or directory
How can I fix this issue, many thanks!

One option would be to pipe output through either sed or tr, to remove carriage returns:
sed -e 's/[\r\n]//g'
or
tr -d '\r' if you only want to remove \r

Related

Show file sizes with names that contain spaces [duplicate]

This question already has answers here:
When to wrap quotes around a shell variable?
(5 answers)
File names with spaces in BASH
(6 answers)
Closed 4 years ago.
Im trying to run trough a directory showing file sizes, but when i try to print the size of a file that has spaces stat command fails. How can i fix this?
#!/bin/bash
for file in /home/user/Desktop/*; do
fileSize=$(stat -c%s $file)
echo $fileSize
done
Use quotes.
#!/bin/bash
for file in /home/user/Desktop/*; do
fileSize=$(stat -c%s "$file")
echo $fileSize
done
Adjusted for my desktop -
$: for file in *
do case "$f" in
*\ *) printf "'$f': %d\n" $(stat -c%s "$file")
esac
done
'BPS Stuff': 0
'New TWC Account - Hodges Paul A.msg': 37888
'Nov FTOTD calendar.JPG': 138769
'OCA Web Client - ASAP.lnk': 2406
'Paul - Copy.png': 64915
'Solstice Client.lnk': 2165
'VIP Access.lnk': 2079
You should add quotes around variables to make sure if there are spaces in there that they get picked up correctly:
#!/bin/bash
for file in /home/user/Desktop/*; do
fileSize=$(stat -c%s "$file")
echo $fileSize
done
What bash does is it simply replaces $var with the thing in $var. If that contains spaces it becomes something else then you intended, because spaces are used in bash to separate command options.
Consider the following example:
file="-l -h -a -F"
ls $file
This gets parsed as:
ls -l -h -a -F
The output will not be the just the file "-l -h -a -F" but it will get parsed as options for ls and it will show the current directory listing. If you had put quotes around $file like so:
file="-l -h -a -F"
ls "$file"
It will get parsed like:
ls "-l -h -a -F"
ls will search for the file "-l -h -a -F" and show only that one file (assuming it exists, it errors otherwise).

How to execute commands read from the txt file using shell? [duplicate]

This question already has answers here:
Run bash commands from txt file
(4 answers)
Closed 4 years ago.
I tried to execute commands read it from txt file. But only 1st command is executing, after that script is terminated. My script file name is shellEx.sh is follows:
echo "pwd" > temp.txt
echo "ls" >> temp.txt
exec < temp.txt
while read line
do
exec $line
done
echo "printed"
if I keep echo in the place of exec, just it prints both pwd and ls. But i want to execute pwd and ls one by one.
o/p am getting is:
$ bash shellEx.sh
/c/Users/Aditya Gudipati/Desktop
But after pwd, ls also need to execute for me.
Anyone can please give better solution for this?
exec in bash is meant in the Unix sense where it means "stop running this program and start running another instead". This is why your script exits.
If you want to execute line as a shell command, you can use:
line="find . | wc -l"
eval "$line"
($line by itself will not allow using pipes, quotes, expansions or other shell syntax)
To execute the entire file including multiline commands, use one of:
source ./myfile # keep variables, allow exiting script
bash myfile # discard variables, limit exit to myfile
A file with one valid command per line is itself a shell script. Just use the . command to execute it in the current shell.
$ echo "pwd" > temp.txt
$ echo "ls" >> temp.txt
$ . temp.txt

Bash read -p doesnt work [duplicate]

This question already has answers here:
': not a valid identifier [duplicate]
(4 answers)
Why would a correct shell script give a wrapped/truncated/corrupted error message? [duplicate]
(1 answer)
Closed 5 years ago.
When running my shell script I get this error:
': not a valid identifiere 17: read: `
Here is my shell script:
#!/usr/bin/env bash
# Mr. Robot Install Wordpress Script
# Script is used for the following:
# add user to server
# change to new user home directory
# download latest version of wordpress
# unzip wordpress
# move all files up a directory level
# move up a directory level
# delete wordpress.zip
# remove wordpress folder
echo "/*****************************************************/"
echo "/************** HELLO MR. ROBOT **********************/"
echo "/*****************************************************/"
echo ".."
echo ".."
echo "Website URL"
echo 'url: \r'
read -p $website
echo 'User: \r'
read -p $newuser
echo 'Pass: \r'
read -p $password
echo "creating account......"
/scripts/wwwacct $website $newuser $password 0 x3 n n n 0 0 0 0 0 0
echo "Changing Directory....."
cd ~/home/$newuser/
echo "Getting Latest Version of Wordpress!"
curl -O http://wordpress.org/latest.tar.gz
echo "Tarball Incoming!!"
tar xvzf latest.tar.gz
echo "removing tar file"
rm latest.tar.gz
echo "moving wordpress folders!"
cp -a ~/home/$newuser/public_html/wordpress/. ~/home/$newuser/public_html/
cd /home/$newuser/public_html/
echo "Part 01 Complete!!"
exit
I've tried to use different versions of the read line with -p or -e. Any help would be appreciated. I've even tried adding it on a separate line with input.
EDIT: Updated file to where it takes inputs, but issue is that the inputs are not being used through the rest of the script. Thus causing errors for directories not being found.
Don't quote the variables names. read needs the name of the variable to assign to, not its value, which is what you get if you have a dollar sign $.
read -p 'website url: ' website
read -p 'Username: ' newuser
read -p 'Password: ' password
It looks like one of the variables holds \r, a carriage return. The error message that bash is trying to print is something like:
bash: ./script: line 17: read: `\r': not a valid identifier
But \r causes the cursor to go back to the beginning of the line, causing ': not a valid identifier to overwrite the beginning of the message.
As mentioned above by John Kugelman, in case you have to check if you Input_file is having carriage returns then you could run following command:
cat -v Input_file
In case you find them then try to remove them from either of following cmmands:
tr -d '\r' < Input_file
OR
awk '{gsub(/\r/,"")} 1' Input_file
Or check if your system(box) has dos2unix utility you could use that also for removing these carriage returns.

Error While running for loop for renaming multiple file in shell script

While renaming multiple file in AIX using for loop I am getting error
${fn/$eisinno/$efilename}": 0403-011 The specified substitution is not valid for this command.
Input File:
raj_10576_INE728J01019_arya1.pdf
ram_10576_INE728J01019_arya1.pdf
rhaul_10576_INE728J01019_arya1.pdf
sanjay_10576_INE728J01019_arya1.pdf
dinesh_10576_INE728J01019_arya1.pdf
Desired Output File:
raj_10576_Remote_sag.pdf
ram_10576_Remote_sag.pdf
rhaul_10576_Remote_sag.pdf
sanjay_10576_Remote_sag.pdf
dinesh_10576_Remote_sag.pdf
My script is as follow:
#!/bin/bash
eisinno="INE728J01019_arya1.pdf"
evenno=10576
efilename="remote_sag.pdf"
cd /home/rishabh/$eveno
for file in *_$eveno_*.pdf
do
mv -i "${file}" "${file/$eveno_$eisinno/$eveno_remote_$efilename}"
done
Kindly help me
Use double n in the evennos and use braces to make sure where a variable ends:
#!/bin/bash
eisinno="INE728J01019_arya1.pdf"
evenno=10576
efilename="remote_sag.pdf"
cd /home/rishabh/${evenno}
for file in *_${evenno}_*.pdf; do
echo "Debug: ${file} ==> ${file/${evenno}_${eisinno}/${evenno}_remote_${efilename}}"
# Alternative:
echo ${file} | sed "s/${evenno}_${eisinno}/${evenno}_remote_${efilename}/"
mv -i "${file}" "${file/${evenno}_${eisinno}/${evenno}_remote_${efilename}}"
done

Bash Script Variable

#!/bin/bash
RESULT=$(grep -i -e "\.[a-zA-z]\{3\}$" ./test.txt)
for i in $(RESULT);
do
echo "$i"
FILENAME="$(dirname $RESULT)"
done
I have a problem with the line FILENAME="$(dirname $RESULT)". Running the script in debugging mode(bash -x script-name), the ouput is:
test.sh: line 9: RESULT: command not found
For some reason, it can't take the result of the variable RESULT and save the output of dir command to the new variable FILENAME. I can't understand why this happens.
After lots of tries, I found the solution to save full path of finame and finame to two different variables.
Now, I want for each finame, find non-case sensitive of each filename. For example, looking for file image.png, it doesn't matter if the file is image.PNG
I am running the script
while read -r name; do
echo "$name"
FILENAME="$(dirname $name)"
BASENAME="$(basename $name)"
done < <(grep -i -e "\.[a-zA-z]\{3\}$" ./test.txt)
and then enter the command:
find . $FILENAME -iname $BASENAME
but it says command FILENAME and BASENAME not found.
The syntax:
$(RESULT)
denotes command substitution. Saying so would attempt to run the command RESULT.
In order to substitute the result of the variable RESULT, say:
${RESULT}
instead.
Moreover, if the command returns more than one line of output this approach wouldn't work.
Instead say:
while read -r name; do
echo "$name"
FILENAME="$(dirname $name)"
done < <(grep -i -e "\.[a-zA-z]\{3\}$" ./test.txt)
The <(command) syntax is referred to as Process Substitution.
for i in $(RESULT) isn't right.You can use $RESULT or ${RESULT}

Resources