Directory name created with a dot ,using shell script - bash

I am using Cygwin Terminal to run shell to execute shell scripts of my Windows 7 system.
I am creating directory , but it is getting created with a dot in name.
test.sh
#!/bin/bash
echo "Hello World"
temp=$(date '+%d%m%Y')
dirName="Test_$temp"
dirPath=/cygdrive/c/MyFolder/"$dirName"
echo "$dirName"
echo "$dirPath"
mkdir -m 777 $dirPath
on executing sh test.sh its creating folder as Test_26062015 while expectation is Test_26062015.Why are these 3 special charterers coming , how can I correct it

Double quote the $dirPath in the last command and add -p to ignore mkdir failures when the directory already exists: mkdir -m 777 -p "$dirPath". Besides this, take care when combining variables and strings: dirName="Test_${temp}" looks better than dirName="Test_$temp".
Also, use this for static analysis of your scripts.
UPDATE: By analyzing the debug output of sh -x, the issue appeared due to DOS-style line-endings in the OP's script. Converting the file to UNIX format solved the problem.

Related

Cygwin BASH script file - unwanted single quotes added automatically to constant string - how to prevent

I have this BASH script which I run in a Cygwin terminal instance via the command
bash -f myfile.sh
All I need it to do is delete all *.txt files in the Cygwin /home/user directory.
#!/bin/bash
set -x
rm -rf /home/user/*.txt
This does not work, running the file (I only added "set -x" to debug when it started failing) shows
+ rm -rf '/home/user/.txt*
The problem is literally that I specify in my code in the Cygwin BASH script
rm -rf /home/user/*.txt
without any quotes, but when ran in Cygwin terminal in the BASH script, it resolves to
rm -rf '/home/user/*.txt'
e.g. single quotes are added by Cygwin BASH.
I've scoured other posts where the responses indicate the quotes are only there due to "set -x" formatting the output to show a unitary string, but without "set -x" in the script file the rm command still fails, e. g. the rm command string IS still quoted (or some other mangling is applied?), and therefore the rm line in the script does not work.
I managed to confirm that by manually running in the Cygwin terminal
rm -rf '/home/user/*.txt'
which does nothing (it just returns, leaving the .txt files intact in /home/user/), and then running
rm -rf /home/user/*.txt
manually, which does work perfectly, deleting all .txt files in the /home/user/ directory under the Cygwin terminal.
How can I get the above command to remove all .txt iles in /home/user/ from inside a Cygwin terminal BASH script file?
Thanks!
As intimated above, the answer to this is to not use -f when calling bash, e. g.
just
bash myfile.sh

Why am I getting if: Expression Syntax when I try to run this script?

I am currently trying to run a Unix Executable File in terminal (my shell is TCSH) I downloaded online and I keep getting the following error:
if: Expression Syntax
Here is the script I am trying to run:
if [ -f .1 ]
then
cc -o xrdcalc .source/xrdcalc-1.1.c -lm
chmod 700 xrdcalc
./xrdcalc
else
platform=`uname`
echo
echo
echo "You are using \"xrdcalc\" for the first time on $platform , read the \"Readme.txt\" file and then proceed"
echo
echo
echo "Press enter...."
read char;
echo `date` > .1
mkdir .source
mv xrdcalc-1.1.c .source
cc -o xrdcalc .source/xrdcalc-1.1.c -lm
./xrdcalc
fi
I have little experience with running scripts and I am sure it is an easy fix.
There are other issues with this code that indicate it was coded for traditional sh or bash.
Just put #!/bin/bash as the first line (using the correct path to your system's copy of bash), and it should work without other modifications.
Of course,
chmod 755 scriptName
is also required to "mark" the file as executable and if the file is saved to a directory not in the path, you need to either cd to the correct dir, or invoke as
/full/path/to/scriptName
If you're using a reduced version of Linux that doesn't have bash installed and you can't install it for some reason, then look for other 'Bourne Shell' derived shell processors, like ash, dash, ksh, sh .
IHTH

Error: Tar command not found

echo "Enter path of backup file e.g /tmp/backup/etc.tar.gz : "
read PATH #input was /tmp/backup/etc.tar.gz
echo "Enter directory: "
read DIR #input was /root/testing
sudo tar -zvxf "$PATH" -C "$DIR"
when I ran the script, it said that the command was not found. I tried using whatever kind of brackets for the variables but still not working. Any help?
However when I ran the command tar -zvxf /tmp/backup/etc.tar.gz -C /root/testing , it worked.
You're saving something into PATH which is what the shell will search to find the executables. So when you use that variable the shell can't find, say, tar because it is no longer in your search path. Use a different variable name.

Shell Scripting - Must not generate extra messages and its not but says I am

There is a similar question about this issue. But not the same solution.
I am to create a shell script that takes two parameters:
1.the desired file extension
2.the name of a single file to be renamed with that extension
The script should rename the file with the desired file extension. If the file does not exist, it should print out "fileName: No such file". It is producing this message but the professor's tests says it is producing unexpected messages(extra messages) but it is not. My shell script is:
#!/bin/sh
fileExtension="$1"
shift
oldName="${#}"
extension=${oldName##*.}
if test -r "$oldName"
then
if "$fileExtension" == $oldName.*
then
echo "$oldName"
else
newName="${oldName%.*}.$fileExtension"
mv "$oldName" "$newName"
fi
else
echo "$oldName": No such file
fi
Everytime I test it, it produces "fileName: no such file" and nothing else.
The test is executed by
./chExt2.sh cpp aardvark.CPP
where aardvark.CPP is not on the directory.
Any help or guidance would be much appreciated. Thank you
Your shebang is telling your shell to use /bin/sh to run the script. /bin/sh is typically a symlink to the real/default shell on the host. For example, mine's bash:
$ ls -l /bin/sh
lrwxrwxrwx 1 root root 4 Mar 27 2009 /bin/sh -> bash
It sounds like your professor's computer's /bin/sh is using a different shell than you are [expecting]. This script runs fine in ksh or bash, for example, but produces "extra output" if /bin/sh is tcsh:
fileExtension=cpp: Command not found.
Illegal variable name.

Bash scripting: mkdir error

I have the following script sample:
#!/bin/bash
# Aborts the script on "simple command failure" (does not cover pipes)
set -e
# Makes sure we do not run the script outside the correct directory (i.e. the backup directory)
projects_directory='~/projects'
backup_drectory="${projects_directory}/backup/"
echo "Backup directory: ${backup_drectory}"
if [ ! -d "$projects_directory" ]; then
mkdir "$projects_directory"
echo "${projects_directory} created successfully"
fi
Which fails miserably with the following output:
Backup directory: ~/projects/backup/
mkdir: cannot create directory `~/projects': No such file or directory
I do not understand why. If I enter the mkdir ~/projects command manually in a Terminal, the directory gets created. Any suggestion is most welcome.
Remove the single quotes:
projects_directory=~/projects
The quoting prevents the shell from expanding the ~ character.

Resources