shell "if" statement - shell

I am new to unix and am practicing a simple script to unzip a load of files within a specified directory. I need the program to move the zipped file into another folder when it is done unzipping it (I called this oldzipped folder). For simplicity, I have removed the part of the code unzipping the file and currently have the program working for a specific file rather than the *tar.7z file extention. For some reason, the mv statement is not working. Unix is saying the following when I try to run the script. Could someone give me a hand with this? Again, I know this is the long way of doing things, but I want practice writing a script. Please be nice, as I am very new to Unix :(
unzip5: line 14: [ASDE0002.tar.7z]: command not found
#!~/bin/bash
# My program to try to unzip several files with ending of tar.7z
# I have inserted the ability to enter the directory where you want this to be done
echo "What file location is required for unzipping?"
read dirloc
cd $dirloc
mkdir oldzippedfiles
for directory in $dirloc
do
if
[ASDE0002.tar.7z]
then
mv -f ASDE0002.tar.7z $dirloc/oldzippedfiles
fi
done
echo "unzipping of file is complete"
exit 0

[ is the name of a (sometimes built-in) command which accepts arguments. As such you need to put a space after it as you would when invoking any other program. Also, you need a test. For example, to determine if the file exists and is a file, you need to use -f:
if [ -f ASDE0002.tar.7z ]
then
mv -f ASDE0002.tar.7z $dirloc/oldzippedfiles
fi
Here are some other possible tests.

Related

Moving a file from a directory to home directory using shell script, it works as a standalone command(as in outside the .sh file)

Below is my .sh file
sh summaryByClient.sh $1 - takes around 10 mins to fetch the required data
mv summary.html ~/public_html/chats/ - **this is not happening**
exit 0
I do not understand why mv summary.html ~/public_html/chats/ this is not working inside .sh file, However I am able to mv separately using the same above command.
Could you be running into the issue from this answer with expanding the user's home directory? What happens if you write your script like this:
#!/bin/bash
# Other tasks to retrieve summary.html done here
mv ./summary.html $HOME/public_html/chats/
exit 0
Also, it's always a good idea to check whether the destination directory exists before a mv command. Examples are shown in this answer

Unix Shell scripting for copying file from one folder to another

Hi this is my shell script to copy files from one directory to another directory with timestamp.But my script shows too many arguments.I want to copy files from one directory to another.What error in my code.
Date_Val="$(date +%Y%m%d%H%M%S)";
cd /etl_mbl/SrcFiles/
if [ -f /etl_mbl/SrcFiles/SrcFiles_TEMP*.csv ]
then
cp /etl_mbl/SrcFiles/SrcFiles_TEMP/*.csv /etl_mbl/SrcFiles/Archive/*_$Date_Val.csv
fi
The reason you got "too many arguments" error is that the wildcard in the "if" statement expands to a multitude of files. Please also note that you cannot have wildcards in the destination of a "cp". You probably want something like this:
#!/bin/bash
Date_Val="$(date +%Y%m%d%H%M%S)";
for file in ./src/*.csv; do
filename=${file##*/}
basename=${filename%.*}
cp $file ./archive/$basename\_$Date_Val.csv
done

assistance with postinstall script on Mac

New at this. I need to use a postinstall script to move a file and a folder to the user's Application Support folder on a Mac. For the file I only want to move it if the file doesn't already exist. I do not want to overwrite it if if does exist. Here is my script. It runs but nothing gets copied. I'm using the Packages app, btw, and this script is loaded into the postinstall script tab.
#!/bin/sh
if ! "/Library/Application Support/MyApp/MyApp user dict"; then
mv "$1/Contents/Resources/MyApp user dict" "/Library/Application Support/MyApp/.";
fi
mv "$1/Contents/Resources/Spellcheck Dictionary" "/Library/Application Support/MyApp/.";
exit 0
User-specific tasks generally do not belong in installer scripts -- remember that there may be multiple users on a machine, and that some of them may not be accessible when your installer is running. (For example, users may have encrypted home directories, or may not exist until after your installer is run.) If your application needs to copy files to the user's home directory, it should probably do this when it is first launched.
Nevertheless, I see several specific issues with this script:
Your script refers to $1 in several places. Are you sure that your script has an argument passed to it on the command line?
The correct syntax to test if a file does not exist is:
if [ ! -f "/path/to/file" ] ; then …
Your script is missing the square brackets and -f condition. (For details, see man test.)
Assuming that $1 is supposed to be the path to the current user's home directory, you have the arguments to mv backwards. The destination comes last, not first. (The syntax is essentially mv from to.)

Bash script to execute only on new files in directory

I'm currently running a Bash script called (log2csv) that runs against a specified .log file. Sitting in the desired directory I can type in terminal:
log2csv Red1_1.log
This will create Red1_1.csv
This is my current bash script:
#!/bin/bash
for path
do
base=$(basename "$path")
noext="${base/.log}"
/Users/joshuacarter/bin/read_scalepack.pl "$path" > "${noext}.csv"
done
This script is actually running a perl script on the specified log and putting the results in a CSV output.
I can alternatively run in terminal:
log2csv *.log
This will run the script against all .log files in the current directory and create .csv files for every one.
What I would like the script to do is only run on .log files that haven't had .csv files created for them. After doing some research I think I possibly can use inotifywait to achieve this, but I'm unsure how to make this work in my script? I also have read that this may be an issue if you overwrite a file. Any help or ideas would be most appreciated!
What I would like the script to do is only run on .log files that haven't had .csv files created for them.
Simply skip those .log files whose corresponding .csv files already exist:
for path
do
base=$(basename "$path")
noext="${base/.log}"
[ -e "${noext}.csv" ] && continue # <---------------
/Users/joshuacarter/bin/read_scalepack.pl "$path" > "${noext}.csv"
done

Rename output of Shell script(s)

I am trying to create a script that runs an another script and changes the name from the output.
Here is the script so far:
#! /bin/bash
i=1
for N in mediainput.iso mediainput2.iso
do
x264transcode $N
mv $N $((i++))
done
This don`t that well. It just moves the files and renames them.
I need to first run the x264transcode and then rename the output of that. Since they all get the same name when x264transcode as processed the files.
Its okey that the name the files are changed to are 1 then 2 and so on.
But it would be a plus if there where a method of getting the name of the folder the file was inside or the file itself. Maybe choosing between them for different scenarios.
Example below:
~/Videos/Summer Vacation 2009/dvd.iso
Output from x264: VIDEO01.mkv
Output from rename script: Summer-Vacation-2009.mkv
Does x264transcode always call its output VIDEO01.mkv? Are all the video files dvd.iso? If so, something like this, to also get the correct filename with hyphens:
cd ~/Videos
for I in */dvd.iso
do
x264transcode $I
mv VIDEO01.mkv `dirname $I|tr ' ' -`.mkv
end
This is assuming x264transcode stores VIDEO01.mkv in the current directory rather than the directory its input file is located in.

Resources